博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BNUOJ 3278 Candies
阅读量:4467 次
发布时间:2019-06-08

本文共 4577 字,大约阅读时间需要 15 分钟。

Candies

Time Limit: 1500ms
Memory Limit: 131072KB
This problem will be judged on 
PKU. Original ID: 
64-bit integer IO format: %lld      Java class name: Main
 
 

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 21 2 52 1 4

Sample Output

5

Source

 
解题:一个很裸的差分约束问题!直接求1到n的最短路
 
至于为什么spfa要用栈而不是队列,因为一直超时!vector没法用,用了就超时!
 
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define LL long long14 #define INF 0x3f3f3f3f15 using namespace std;16 struct arc{17 int to,w,next;18 };19 arc g[200000];20 int head[30010],tot,n,m;21 int d[30010];22 bool done[30010];23 void add(int u,int v,int w){24 g[tot].to = v;25 g[tot].w = w;26 g[tot].next = head[u];27 head[u] = tot++;28 }29 stack
stk;30 void spfa(int s){31 d[s] = 0;32 while(!stk.empty()) stk.pop();33 done[s] = true;34 stk.push(s);35 int u,i;36 while(!stk.empty()){37 u = stk.top();38 stk.pop();39 done[u] = false;40 for(i = head[u]; i != -1; i = g[i].next){41 if(d[g[i].to] > d[u] + g[i].w){42 d[g[i].to] = d[u] + g[i].w;43 if(!done[g[i].to]){44 done[g[i].to] = true;45 stk.push(g[i].to);46 }47 }48 }49 }50 }51 int main(){52 int i,j,u,v,w;53 while(~scanf("%d %d",&n,&m)){54 for(i = 0; i <= n; i++){55 done[i] = false;56 head[i] = -1;57 d[i] = INF;58 }59 for(tot = i = 0; i < m; i++){60 scanf("%d %d %d",&u,&v,&w);61 add(u,v,w);62 }63 spfa(1);64 printf("%d\n",d[n]);65 }66 return 0;67 }
View Code

 

好吧,加个优先队列版的Dijkstra吧

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define LL long long14 #define INF 0x3f3f3f3f15 #define pii pair
16 using namespace std;17 struct arc{18 int to,w,next;19 };20 arc g[200000];21 int head[30010],d[30010],tot,n,m;22 bool done[30010];23 void add(int u,int v,int w){24 g[tot].to = v;25 g[tot].w = w;26 g[tot].next = head[u];27 head[u] = tot++;28 }29 priority_queue
< pii >,greater< pii > >q;30 void dijkstra(int s,int t){31 while(!q.empty()) q.pop();32 d[s] = 0;33 q.push(make_pair(d[s],s));34 while(!q.empty()){35 int u = q.top().second;36 q.pop();37 if(done[u]) continue;38 done[u] = true;39 for(int i = head[u]; i != -1; i = g[i].next){40 if(d[g[i].to] > d[u] + g[i].w){41 d[g[i].to] = d[u] + g[i].w;42 q.push(make_pair(d[g[i].to],g[i].to));43 }44 }45 if(done[t]) return;46 }47 }48 int main(){49 int i,u,v,w;50 while(~scanf("%d %d",&n,&m)){51 for(i = 0; i <= n; i++){52 head[i] = -1;53 done[i] = false;54 d[i] = INF;55 }56 for(tot = i = 0; i < m; i++){57 scanf("%d %d %d",&u,&v,&w);58 add(u,v,w);59 }60 dijkstra(1,n);61 printf("%d\n",d[n]);62 }63 return 0;64 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3906487.html

你可能感兴趣的文章
python之路-基础篇-第七周
查看>>
高性能队列Disruptor系列2--浅析Disruptor
查看>>
ViurtualBox配置虚拟机Linux的网络环境
查看>>
VLC 媒体播放器
查看>>
勿忘国耻2018/09/18
查看>>
Jenkins部署码云SpringBoot项目
查看>>
多标签分类(multi-label classification)综述
查看>>
史上最全面的Spring-Boot-Cache使用与整合
查看>>
图的遍历(深度优先与广度优先搜索两种方案)
查看>>
快速读入模板
查看>>
把一张图片变成base64
查看>>
impdp报错: ORA-39064: 无法写入日志文件 ORA-29285: 文件写入错误
查看>>
\n ^ \t的使用
查看>>
css盒模型
查看>>
计算机学科各专业大牛
查看>>
SIM卡
查看>>
探索式测试:测试自动化
查看>>
用 UIWebView 代替 UITextView,解决行间距问题
查看>>
学习秦九韶算法
查看>>
Mysql中use filesort的误区
查看>>