• poj--3159--Candies(简单差分约束)


    Candies
    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 26888   Accepted: 7398

    Description

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

    snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

    Input

    The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 throughN. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers AB and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.

    Output

    Output one line with only the largest difference desired. The difference is guaranteed to be finite.

    Sample Input

    2 2
    1 2 5
    2 1 4

    Sample Output

    5

    Hint

    32-bit signed integer type is capable of doing all arithmetic.

    Source

    数据有点多,不能用cin输入,会超时!!队列也不可以用,也会超时,数组模拟栈就行
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define MAXN 30010
    #define MAXM 150000
    #define INF 0x3f3f3f
    int head[MAXN],vis[MAXN],dis[MAXN],Instack[MAXN];
    int n,m,cnt;
    struct node
    {
    	int u,v,val;
    	int next;
    }edge[MAXM];
    void init()
    {
    	memset(head,-1,sizeof(head));
    	cnt=0;
    }
    void add(int u,int v,int val)
    {
    	node E={u,v,val,head[u]};
    	edge[cnt]=E;
    	head[u]=cnt++;
    }
    void getmap()
    {
    	while(m--)
    	{
    		int a,b,c;
    //		cin>>a>>b>>c;
    		scanf("%d%d%d",&a,&b,&c);
    		add(a,b,c);
    	}
    }
    void SPFA()
    {
    	memset(vis,0,sizeof(vis));
    	memset(Instack,0,sizeof(Instack));
    	memset(dis,INF,sizeof(dis));
    	dis[1]=0;
    	vis[1]=1;
    	int top=0;
    	Instack[top++]=1;
    	while(top)
    	{
    		int u=Instack[--top];
    		vis[u]=0;
    		for(int i=head[u];i!=-1;i=edge[i].next)
    		{
    			node E=edge[i];
    			if(dis[E.v]>dis[u]+E.val)
    			{
    				dis[E.v]=dis[u]+E.val;
    				if(!vis[E.v])
    				{
    					vis[E.v]=1;
    					Instack[top++]=E.v;
    				}
    			}
    		}
    	}
    	cout<<dis[n]<<endl;
    }
    int main()
    {
    	while(cin>>n>>m)
    	{
    		init();
    		getmap();
    		SPFA();
    	}
    	return 0;
    }


  • 相关阅读:
    C#创建线程
    Halcon算子
    二叉树的层次遍历
    反转单链表
    “开-闭”原则 (Open-Closed principle, OCP)
    CSUOJ1867 John and Health rate
    LOCAL_MODULE_TAGS
    void * kmalloc(size_t size, int flags)
    printk(Loglevels string)
    container_of宏定义解析
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273535.html
Copyright © 2020-2023  润新知