Language:Default
Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 43021   Accepted: 12075

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 through N. 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

  1. 2 2
  2. 1 2 5
  3. 2 1 4

Sample Output

  1. 5

Hint

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

Source

题意:幼儿园有n个小朋友分糖果,现在有m个如下形式的条件需要满足: a b c 表示b同学糖果数-a同学糖果数<=c. 现在问你满足m个条件的情况下,要使得n号同学糖果数-1号同学糖果数的差值最大为多少?

分析:

首先对于m个条件来说,如果b-a<=c,那么从a到b有一条长c的边.现在我们要求的是d[n]与d[1]的差距最大,所以初始化应该令d[1]=0,且d[i]=INF( i>0). (根据百度百科对差分约束的介绍)

又由于该题中的c值都是正数,所以不会存在负权路或环.所以直接Dijkstra求1号点到其他所有点的最短距离即可得到解:d[n]-d[1].

根据算法导论的讲解,其实差分约束本来就是用最短路求解的.不过存在负权环的情况,所以用BellmanFord算法还可以判断出无解的情况.

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<queue>
  5. #define INF 1e9
  6. using namespace std;
  7. const int maxn=30000+10;
  8. const int maxm=150000+10;
  9. struct Edge
  10. {
  11.     int from,to,dist;
  12.     Edge(){}
  13.     Edge(int f,int t,int d):from(f),to(t),dist(d){}
  14. };
  15.  
  16. struct HeapNode
  17. {
  18.     int d,u;
  19.     HeapNode(int d,int u):d(d),u(u){}
  20.     bool operator<(const HeapNode &rhs)const
  21.     {
  22.         return d>rhs.d;
  23.     }
  24. };
  25.  
  26. struct Dijkstra
  27. {
  28.     int n,m;
  29.     int head[maxn],next[maxm];
  30.     Edge edges[maxm];
  31.     int d[maxn];
  32.     bool done[maxn];
  33.  
  34.     void init(int n)
  35.     {
  36.         this->n=n;
  37.         m=0;
  38.         memset(head,-1,sizeof(head));
  39.     }
  40.  
  41.     void AddEdge(int from,int to,int dist)
  42.     {
  43.         edges[m]=Edge(from,to,dist);
  44.         next[m]=head[from];
  45.         head[from]=m++;
  46.     }
  47.  
  48.     int dijkstra()
  49.     {
  50.         priority_queue<HeapNode> Q;
  51.         for(int i=0;i<n;i++) d[i]= i==0?0:INF;
  52.         memset(done,0,sizeof(done));
  53.         Q.push(HeapNode(d[0],0));
  54.  
  55.         while(!Q.empty())
  56.         {
  57.             HeapNode x=Q.top(); Q.pop();
  58.             int u=x.u;
  59.             if(done[u]) continue;
  60.             done[u]=true;
  61.             for(int i=head[u];i!=-1;i=next[i])
  62.             {
  63.                 Edge &e=edges[i];
  64.                 if(d[e.to]>d[u]+e.dist)
  65.                 {
  66.                     d[e.to]= d[u]+e.dist;
  67.                     Q.push(HeapNode(d[e.to],e.to));
  68.                 }
  69.             }
  70.         }
  71.         return d[n-1];
  72.     }
  73.  
  74. }DJ;
  75.  
  76. int main()
  77. {
  78.     int n,m;
  79.     scanf("%d%d",&n,&m);
  80.     DJ.init(n);
  81.     while(m--)
  82.     {
  83.         int u,v,d;
  84.         scanf("%d%d%d",&u,&v,&d);
  85.         u--,v--;
  86.         DJ.AddEdge(u,v,d);
  87.     }
  88.     printf("%d\n",DJ.dijkstra());
  89.     return 0;
  90. }

图论--差分约束--POJ 3159 Candies的更多相关文章

  1. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  2. 图论--差分约束--POJ 1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  3. 图论--差分约束--POJ 2983--Is the Information Reliable?

    Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...

  4. 图论--差分约束--POJ 1201 Intervals

    Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30971 Accepted: 11990 Descripti ...

  5. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  6. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  7. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  8. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  9. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

随机推荐

  1. NHibernate COUNT(*) 统计问题

    NHibernate这个框架用了有一年多了,相对有很大的优势,可以省去很多写Sql的时间. 但是如果你想用它做统计,那么有点抱歉,只能手动写写了.它内置的东西很难符合你的需求. 我遇到的问题是这样的. ...

  2. Win8.1/Win10在某些程序输入中文变成问号的解决方法

    之前我是使用Win8.1,在某些软件上输入中文,却显示问号,换输入法也没用,当时也没用太在意,后来升级到Win10还是一样.同样的软件在其它Win8.1/Win10电脑却可以正常显示中文. 解决方法如 ...

  3. hexo部署在码云中 无样式问题

    在本地localhost:4000 运行如下 上传码云之后打开Gitee Pages服务 如下 同时控制台打印 解决方法 找到根目录下的_config.yml中的url 和 root # url: h ...

  4. **********Prometheus(三)******************

    通过centos7.x来部署Prometheus ####同步时间,否则后面监控会有异常!!!!!####### 1. 创建文件夹,上传软件包.解压并将prometheus promtool两个命令复 ...

  5. Salesforce 产品 | 协同办公“大魔王”,Salesforce Quip的使用攻略!

    Salesforce帮助企业渡过疫情难关,支持在线远程办公.7.5亿美金收购的动态文档共享平台Quip,即刻开放给所有Salesforce老客户还有非营利组织免费使用至2020年9月30日. Quip ...

  6. 小白们错过就没了!Python基础之注释&变量命名

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:DZQTesters PS:如有需要Python学习资料的小伙伴可以加 ...

  7. work of 12/30/2015

    part 组员                今日工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云   merge UI 与reader     6  丰富re ...

  8. PHP出现SSL certificate:unable to get local issuer certificate的解决办法

    当本地curl需要访问https时,如果没有配置证书,会出现SSL certificate: unable to get local issuer certificate错误信息. 解决办法: 1.下 ...

  9. Tensorflow 模型线上部署

    获取源码,请移步笔者的github: tensorflow-serving-tutorial 由于python的灵活性和完备的生态库,使得其成为实现.验证ML算法的不二之选.但是工业界要将模型部署到生 ...

  10. JAVA快速排序代码实现

    通过一趟排序将要排序的数据分割成独立的两部分:分割点左边都是比它小的数,右边都是比它大的数.然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列. 快速 ...