poj3159(spfa)
来源:互联网 发布:大数据下的中国 编辑:程序博客网 时间:2024/06/17 23:44
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
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
Hint
贴一个前向星的spfa的模板:
const int INF=100000000,N=1600000;///N表示点的最多数量int head[N],dis[N];bool vis[N];int ip;struct data{ int to,next,w;}tu[N*N];void init(){ ip=0; memset(head,-1,sizeof(head));}void add(int u,int v,int w){ tu[ip].to=v,tu[ip].w=w,tu[ip].next=head[u],head[u]=ip++;}void spfa(int s,int n){ queue<int>q; for (int i=0; i<=n; i++) dis[i]=INF; memset(vis,0,sizeof(vis)); q.push(s); dis[s]=0; while(!q.empty()) { int h=q.front(); q.pop(); vis[h]=0; for (int i=head[h]; i!=-1; i=tu[i].next) { int v=tu[i].to; int w=tu[i].w; if (dis[v]>dis[h]+w) { dis[v]=dis[h]+w; if (!vis[v]) { vis[v]=1; q.push(v); } } } }}初学者理解spfa的过程建议望多看代码,多加一些输出看到数据的具体过程。spfa有两种写法,用栈和队列都能写,只是有时候因为出题的数据原因会有不稳定现象,不过到目前为止,我做过的spfa的题一般队列都能过,有个别的TLE的改成栈也就过了,其实没那么难,主要在广搜过程的理解,没什么难度。
下面是我这题ac代码:
#include <iostream>#include<stdio.h>#include<string.h>using namespace std;const int INF=100000000;int head[1600000],dis[1600000],hhh[1600000];bool vis[1600000];struct data{ int to,next,w;}x[1600000];void spfa(int s,int n){ int r=0; for (int i=0; i<=n; i++) dis[i]=INF; memset(vis,0,sizeof(vis)); hhh[r++]=s; dis[s]=0; while(r) { int h=hhh[--r]; vis[h]=0; for (int i=head[h]; i!=-1; i=x[i].next) { int v=x[i].to; int w=x[i].w; if (dis[v]>dis[h]+w) { dis[v]=dis[h]+w; if (!vis[v]) { vis[v]=1; hhh[r++]=v; } } } } printf("%d\n",dis[n]);}int main(){ memset(head,-1,sizeof(head)); int n,m,cnt=0; scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); x[i].to=b,x[i].next=head[a],x[i].w=c,head[a]=cnt++; } spfa(1,n); return 0;}
- poj3159(spfa+栈)
- poj3159(spfa)
- POJ3159::SPFA
- POJ3159差分约束(SPFA)
- poj3159 Candies(SPFA+stack)
- 【spfa(栈)或者 堆优dijkstra】poj3159
- poj3159——Candies(差分约束+SPFA堆栈)
- POJ3159 Candies(差分约束+SPFA的栈实现)
- spfa+队列+邻接表POJ3159
- POJ3159---Candies(最短路:spfa+栈or队列)(邻接表)
- spfa+栈+邻接表模板POJ3159
- poj3159-Candies差分约束-spfa+栈
- poj3159
- poj3159
- poj3159
- [poj3159]
- poj3159 Candies 差分约束+spfa+模拟stack
- SPFA最短路 建图模板 POJ1511&&POJ3159
- 深入浅出 EJB3.1(下)
- 7 Strace Examples to Debug the Execution of a Program in Linux
- jQuery Ajax 实例
- 机房收费系统之结账
- 【Java】-基础-数据库操作
- poj3159(spfa)
- Androd的IPC通信(二)
- 分享一些不错的学习IOS资源网站
- 数据结构中排序方法基本概念 及 分类
- 黑马程序员——IDE&Object类、常用API-String类、常用API-Arrays、System、StringBuffer&Integer类和Date类、对象数组
- poj1734Sightseeing trip floyd最小环
- 汤姆猫实现代码
- Lowest Common Ancestor of a Binary Tree
- HDU 1542 Atlantis(线段树扫描线,面积并)