poj--3159--Candies(简单差分约束)
2016-01-11 16:48
423 查看
Candies
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 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
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
Sample Output
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
POJ Monthly--2006.12.31, Sempr
数据有点多,不能用cin输入,会超时!!队列也不可以用,也会超时,数组模拟栈就行
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 26888 | Accepted: 7398 |
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 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
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
数据有点多,不能用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 <<endl; } int main() { while(cin>>n>>m) { init(); getmap(); SPFA(); } return 0; }
相关文章推荐
- 热词
- 仿微信 IM即时通讯基于XMPP服务器的APP源码出售
- android手动更新demo,当然最好还是用第三方,简单方便
- 将cxGrid中的数据导出
- WordPress中重置文章循环的rewind_posts()函数讲解
- easyui的datagrid中editor和combogrid的结合使用
- socket通信中select函数的使用和解释
- 如何在QT里使用Maliit软键盘
- Linux 下curl模拟Http 的get or post请求
- 如何去除移动端链接a标签产生带色边框
- 张小龙首次公开演讲(官方无删减版)
- 异常处理汇总 ~ 修正果带着你的Net飞奔吧!
- 在四合院里写code是什么体验(非拉仇恨)
- poj--3159--Candies(简单差分约束)
- SharePoint自动化系列——Site/Web/List级别的导航菜单
- MySQL 文本文件的导入导出数据的方法
- 给TextView设置Span
- OM-Internal Sales Order (ISO) Process Flow
- 【Android工具】Android Studio插件试水篇
- SQL SERVER中强制类型转换cast和convert的区别