POJ 3159 Candies(spfa、差分约束)

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

2 2
1 2 5
2 1 4

Sample Output

5

题意给你n个熊孩子,现在给熊孩子们分配蜡烛,这n个熊孩子会提出m个要求,每个要求由a,b,c三个整数表示,意思是b孩子的蜡烛最多不能比a孩子多c个。

问你最后满足这样的要求后,第一个孩子和最后一个孩子最多会多拿多少个蜡烛。

这个提一开始想复杂了,首先意识到的就是松弛操作和这个题的描述差不多。
d[b]-d[a]<=dis[a,b]与松弛操作if (d[b]>d[a]+dis[a,b] d[b]=d[a]+dis[a,b]一样!
就是在a,b两点之间建一条长度为后来看了大家的题解说spfa+queue会超时,只能手写堆栈。

代码如下

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 #include <string>
 5 #include <cstdio>
 6 using namespace std;
 7 const int Maxn=30010;
 8 const int Maxe=150010;
 9 const int inf =0x3f3f3f3f;
10 int n,m;
11 int tot;
12 int head[Maxn];
13 bool vis[Maxn];
14 int d[Maxn];
15 int Q[Maxn];
16 struct Edge
17 {
18     int to;
19     int dis;
20     int nxt;
21 }edge[Maxe];
22 void add (int a,int b,int c)
23 {
24     edge[tot].to=b;
25     edge[tot].dis=c;
26     edge[tot].nxt=head[a];
27     head[a]=tot++;
28 }
29 void spfa (int start,int n)
30 {
31     int top=0;
32     for (int v=1;v<=n;++v){
33         if (v==start){
34             Q[top++]=v;
35             vis[v]=true;
36             d[v]=0;
37         }
38         else{
39             vis[v]=false;
40             d[v]=inf;
41         }
42     }
43     while (top!=0){
44         int u=Q[--top];//top是盖子的位置
45         vis[u]=false;
46         for (int i=head[u];i!=-1;i=edge[i].nxt){
47             int v=edge[i].to;
48             if (d[v]>d[u]+edge[i].dis){
49                 d[v]=d[u]+edge[i].dis;
50                 if (!vis[v]){
51                     vis[v]=true;
52                     Q[top++]=v;
53                 }
54             }
55         }
56     }
57 }
58 int main()
59 {
60     //freopen("de.txt","r",stdin);
61     while (~scanf("%d%d",&n,&m)){
62         tot=0;
63         memset(head,-1,sizeof head);
64         for (int i=0;i<m;++i){
65             int a,b,c;
66             scanf("%d%d%d",&a,&b,&c);
67             add(a,b,c);
68         }
69         spfa(1,n);
70         printf("%d\n",d[n]);
71     }
72     return 0;
73 }

 

转载于:https://www.cnblogs.com/agenthtb/p/6418630.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值