标签:
Tyvj 1176 火焰巨魔的惆怅
5 4
1 2 -3
1 3 -6
3 4 1
4 5 -9
4
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; const int maxn = 100005; const int maxint = ~0U>>1; struct edge{ int v; int w; }; int n,m,vis[maxn],d[maxn]; vector<edge> g[maxn]; void input(){ cin>>n>>m; int u,v,w; edge tmp; for(int i = 1;i <= m;i++){ scanf("%d%d%d",&u,&v,&w); tmp.v = u; tmp.w = w; g[v].push_back(tmp); } for(int i = 1;i <= n;i++){ vis[i] = 0; d[i] = maxint; } } void spfa(){ queue<int> q; q.push(n); d[n] = 1; vis[n] = 1; int now,to,add; while(!q.empty()){ now = q.front(); q.pop(); vis[now] = 0; for(int i = 0;i < g[now].size();i++){ to = g[now][i].v; add = (d[now] - g[now][i].w) >> 1; if((d[now]-g[now][i].w) & 1) add++; if(add <= 0) add = 1; if(d[to] > add){ d[to] = add; if(!vis[to]){ vis[to] = 1; q.push(to); } } } } } int main(){ input(); spfa(); cout<<d[1]; return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5811828.html