poj3159——Candies
2010-11-05 21:01
239 查看
差分约束系统问题
果然,普通的dij过不了
采用学长的意见,用SPFA+堆栈水过
注意:spfa过程中,vis[k]=1重新考虑下一轮是否再入栈
果然,普通的dij过不了
采用学长的意见,用SPFA+堆栈水过
注意:spfa过程中,vis[k]=1重新考虑下一轮是否再入栈
#include<stdio.h> #include<string.h> #define max 150005 #define N 30005 #define maxcost 1000000000 struct node { int to,val; int next; }e[max]; int head ,dist ,vis ,stack[max*4],top; int n,m,index; void spfa() { int i,j,min,k; top++; stack[top]=1; for(i=1;i<=n;i++) { dist[i]=maxcost;vis[i]=1; } dist[1]=0;vis[1]=0; while(top) { k=stack[top]; top--; for(i=head[k];i;i=e[i].next ) { if(dist[e[i].to ]>dist[k]+e[i].val ) { dist[e[i].to ]=dist[k]+e[i].val ; if(vis[e[i].to ]) { top++; stack[top]=e[i].to ; vis[e[i].to ]=0; } } } vis[k]=1; } printf("%d/n",dist ); } int main() { int i,a,b,c; index=1; scanf("%d%d",&n,&m); while(m--) { scanf("%d%d%d",&a,&b,&c); e[index].to=b; e[index].val =c; e[index].next =head[a]; head[a]=index++; } spfa(); return 0; }
相关文章推荐
- POJ - 3159 Candies(差分约束系统)
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 Candies 差分约束系统
- Poj 3159 Candies(最短路)
- POJ 3159 Candies 【差分约束】
- POJ 3159 Candies(差分约束+dijkstra+heap)
- POJ 题目3159 Candies(差分约束)
- POJ 3159 Candies 差分约束系统
- POJ 3159 Candies
- POJ 3159 Candies 还是差分约束(栈的SPFA)
- POJ-3159 Candies
- POJ 3159 Candies(差分约束+SPFA )
- 【POJ 3159】Candies
- POJ 3159 Candies(差分约束 转spfa+stack 求最短路径)
- poj 3159 Candies
- poj--3159--Candies(简单差分约束)
- POJ 3159 Candies
- poj 3159 Candies 差分约束
- POJ 3159 Candies(差分约束系统)