POJ3159(最短路)
2016-01-24 11:38
344 查看
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 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 overc 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
下面是用栈实现的spfa算法。用队列实现会TLE。
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 27051 | Accepted: 7454 |
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 overc 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个孩子,再给出M个限制。A,B,c表示孩子B的糖果最多比孩子A的糖果多c个。问N号孩子最多比1号孩子多多少的糖果。 思路:转化为求1号结点到N号结点的最短路问题。
/* dijkstra Accepted 3112KB 547ms G++ */ #include"cstdio" #include"cstring" #include"queue" using namespace std; const int MAXN=150005; const int INF=0x3fffffff; struct Edge{ int to,cost,next; }es[MAXN]; struct P{ int fi,se; P(int cfi,int cse):fi(cfi),se(cse){} bool operator<(const P& a) const { return fi > a.fi; } }; int heap[MAXN]; int V,E; void add_edge(int u,int v,int co) { es[E].to=v; es[E].cost=co; es[E].next=heap[u]; heap[u]=E; E++; } int d[MAXN]; int dijkstra(int s) { for(int i=1;i<=V;i++) d[i]=INF; priority_queue<P> que; que.push(P(0,s)); d[s]=0; while(!que.empty()) { P p=que.top();que.pop(); int v=p.se; if(d[v]<p.fi) continue; for(int i=heap[v];i!=-1;i=es[i].next) { Edge e=es[i]; if(d[e.to]>d[v]+e.cost) { d[e.to]=d[v]+e.cost; que.push(P(d[e.to],e.to)); } } } return d[V]; } int main() { int N,M; while(scanf("%d%d",&N,&M)!=EOF) { memset(heap,-1,sizeof(heap)); V=N,E=0; for(int i=0;i<M;i++) { int u,v,co; scanf("%d%d%d",&u,&v,&co); add_edge(u,v,co); } int ans=dijkstra(1); printf("%d\n",ans); } return 0; }
下面是用栈实现的spfa算法。用队列实现会TLE。
/* spfa Accepted 3112KB 547ms G++ */ #include"cstdio" #include"cstring" using namespace std; const int MAXN=150005; const int INF=0x3fffffff; struct Edge{ int to,cost,next; }es[MAXN]; int stack[MAXN],top; int head[MAXN]; int V,E; void add_edge(int u,int v,int co) { es[E].to=v; es[E].cost=co; es[E].next=head[u]; head[u]=E; E++; } int d[MAXN]; int vis[MAXN]; int spfa(int s) { for(int i=1;i<=V;i++) d[i]=INF; memset(vis,0,sizeof(vis)); top=0; stack[top++]=s; vis[1]=s,d[s]=0; while(top!=0) { int v=stack[--top]; vis[v]=0; for(int i=head[v];i!=-1;i=es[i].next) { Edge e=es[i]; if(d[e.to]>d[v]+e.cost) { d[e.to]=d[v]+e.cost; if(!vis[e.to]) { vis[e.to]=1; stack[top++]=e.to; } } } } return d[V]; } int main() { int N,M; while(scanf("%d%d",&N,&M)!=EOF) { memset(head,-1,sizeof(head)); V=N,E=0; for(int i=0;i<M;i++) { int u,v,co; scanf("%d%d%d",&u,&v,&co); add_edge(u,v,co); } int ans=spfa(1); printf("%d\n",ans); } return 0; }
相关文章推荐
- C# 窗口最大化但不占用任务栏位置
- html5与html4的区别
- 树套树【bzoj3262】陌上花开
- 张江男的逆袭,我如何使用leangoo提升团队效率
- 数据结构实验之栈三:后缀式求值
- 侧边栏增强版
- UILabel---.tff格式字体
- 实现文本、数字、英文字母自动换行!
- struts2初探
- Cacti基于SNMP服务添加监控远程Linux主机
- tomcat 配置 quercus记录:php使用连接池访问数据库
- 【C#】ADO .Net Entities Framework在WPF TreeView中的应用
- - (UIImage*)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapH
- ios中图片不显示的问题
- python日志记录
- 【解题报告】硬币问题
- linux命令笔记之ls
- 论文笔记:Ten years of pedestrian detection, what have we learned?
- Android记录16-友盟第三方登录、分享实现
- 【教程】Android 夜间模式库 changeskin 使用总结