标签:http get ble const struct 约束 ios sdi can
POJ - 3159
Sample Input
2 2 1 2 5 2 1 4
Sample Output
5
/* 差分约束裸题。 差分约束的基本模式为,给出若干个形如a-b<=c的不等式,问你x-y的最大值是多少 那么对每个不等式a-b<=c,连一条由b指向a权值为c的有向边,所有的不等式构成一个图 那么答案就是y到x的最短路 */ #include<iostream> #include<stack> #include<cstdio> #include<cstring> using namespace std; const int maxn=3000010; const int maxm=3500000; const int INF=999999; int n,m,head[maxn],num,dis[maxn]; bool vis[maxn]; struct node{ int to,v,pre; }e[maxn]; void add(int from,int to,int v){ e[num].to=to; e[num].v=v; e[num].pre=head[from]; head[from]=num++; } void spfa(){ int s=1; stack<int>q; q.push(s); vis[s]=1; dis[s]=0; while(!q.empty()){ int cur=q.top(); q.pop(); vis[cur]=0; for(int i=head[cur];i!=-1;i=e[i].pre){ int id=e[i].to; if(dis[id]>dis[cur]+e[i].v){ dis[id]=dis[cur]+e[i].v; if(!vis[id]){ q.push(id); vis[id]=1; } } } } } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++){ head[i]=-1; dis[i]=INF; vis[i]=0; } num=0; for(int i=0;i<m;i++){ int from,to,v; scanf("%d%d%d",&from,&to,&v); add(from,to,v); } spfa(); printf("%d\n",dis[n]); } return 0; }
标签:http get ble const struct 约束 ios sdi can
原文地址:http://www.cnblogs.com/thmyl/p/7266402.html