poj1741,poj3159 Candies

 2023-11-07 阅读 23 评论 0

摘要:地址:http://poj.org/problem?id=3159 題目: Candies Time Limit:?1500MS?Memory Limit:?131072KTotal Submissions:?31463?Accepted:?8782 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teac

地址:http://poj.org/problem?id=3159

題目:

Candies
Time Limit:?1500MS?Memory Limit:?131072K
Total Submissions:?31463?Accepted:?8782

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?A,?B?and?c?in order, meaning that kid?A?believed that kid?B?should never get over?c?candies more than he did.

Output

poj1741?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

POJ Monthly--2006.12.31, Sempr
?
思路:
  差分約束:對于每個信息 x y z,建一條從x到y的權值為z的邊。然后跑最短路。
  別用queue,會T。要用stack。
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4 
 5 using namespace std;
 6 
 7 #define MP make_pair
 8 #define PB push_back
 9 typedef long long LL;
10 typedef pair<int,int> PII;
11 const double eps=1e-8;
12 const double pi=acos(-1.0);
13 const int K=1e6+7;
14 const int mod=1e9+7;
15 
16 struct node
17 {
18     int to,v,next;
19 }edge[K];
20 int tot,head[K];
21 void add(int x,int y,int z)
22 {
23     edge[tot].to=y,edge[tot].v=z,edge[tot].next=head[x];
24     head[x]=tot++;
25 }
26 int vis[K],dis[K],sk[K];
27 void spfa(void)
28 {
29     int cnt=0;
30     sk[cnt++]=1,vis[1]=1,dis[1]=0;
31     while(cnt)
32     {
33         int u=sk[--cnt];
34         vis[u]=0;
35         for(int i=head[u];~i;i=edge[i].next)
36         {
37             int v=edge[i].to,w=edge[i].v;
38             if(dis[v]>dis[u]+w)
39             {
40                 dis[v]=dis[u]+w;
41                 if(!vis[v])
42                     sk[cnt++]=v,vis[v]=1;
43             }
44         }
45     }
46 }
47 int main(void)
48 {
49     int n,m;
50     while(~scanf("%d%d",&n,&m))
51     {
52         tot=0;
53         for(int i=1;i<=n;i++)
54             head[i]=-1,vis[i]=0,dis[i]=0x3f3f3f3f;
55         for(int i=1,u,v,w;i<=m;i++)
56             scanf("%d%d%d",&u,&v,&w),add(u,v,w);
57         spfa();
58         printf("%d\n",dis[n]);
59     }
60     return 0;
61 }

?

轉載于:https://www.cnblogs.com/weeping/p/6973408.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/167089.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息