zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    查找list中的重复数据,并得到不重复数据索引位置
    sql server 存储过程中使用事务
    C#获取web.config配置文件内容
    SendKeys.Send 方法
    C# 获取当前路径方法(转载)
    asp.net中URL参数传值中文乱码的三种解决办法
    无法 连接到SQLEXPRESS 已成功与服务器建立连接,但是在登录过程中发生错误。管道的另一端无任何进程
    Windows7 IIS7 无法启动计算机上的服务W3SVC如何修复,计算机上无法找到.was解决方案 visita iis 7.0
    EXCEL开发Interior.ColorIndex 色彩列表
    asp.net web 登录文本框的回车设计
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273535.html
Copyright © 2011-2022 走看看