您的位置:首页 > 其它

poj3159——Candies

2010-11-05 21:01 239 查看
差分约束系统问题

果然,普通的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: