poj 3159 差分约束

时间:2022-02-20 08:28:08

思路:班长的糖果要比snoopy的多。并且要用手写堆栈,且堆栈的大小要开到20000000.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 1000000000
#define Maxn 30110
#define Maxm 160000
using namespace std;
int index[Maxn],dis[Maxn],vi[Maxn],e,n,Que[];
struct Edge{
int to,next,val;
}edge[Maxm];
void init()
{
memset(vi,,sizeof(vi));
memset(index,-,sizeof(index));
for(int i=;i<Maxn;i++)
dis[i]=inf;
e=;
}
void addedge(int from,int to,int val)
{
edge[e].to=to;
edge[e].val=val;
edge[e].next=index[from];
index[from]=e++;
}
int spfa(int u)
{
int i,j,temp,now,head;
head=;
dis[u]=;
Que[head++]=u;
while(head)
{
temp=Que[--head];
vi[temp]=;
for(i=index[temp];i!=-;i=edge[i].next)
{
now=edge[i].to;
if(dis[temp]+edge[i].val<dis[now])
{
dis[now]=dis[temp]+edge[i].val;
if(!vi[now])
Que[head++]=now;
vi[now]=;
}
}
}
return dis[n];
}
int main()
{
int m,i,j,a,b,c;
scanf("%d%d",&n,&m);
init();
for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
int ans;
ans=spfa();
printf("%d\n",ans);
return ;
}