1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include<bits/stdc++.h> #define int long long #ifdef DEBUG # define debug(...) fprintf(stderr,__VA_ARGS__) #else # define debug #endif using namespace std; bool Begin; const int max_n=200005,inf=1000000000000015LL; inline int read(){ int x=0;bool w=0;char c=getchar(); while(!isdigit(c)) w|=c=='-',c=getchar(); while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar(); return w?-x:x; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10^48); } struct Graph{ int hd[max_n],to[max_n],nx[max_n],ln[max_n],ct; Graph(){ct=1;} inline void add(int u,int v,int w){ nx[++ct]=hd[u],hd[u]=ct,to[ct]=v,ln[ct]=w; } }e;
int n,m,S,f[max_n][2],deg[max_n];
struct node{ int u,op,x; node(int U=0,int OP=0,int X=0):u(U),op(OP),x(X){} bool operator < (const node &b) const{ node a=*this; if(a.x==b.x && a.u==b.u) return a.op<b.op; if(a.x==b.x) return a.u<b.u; return a.x>b.x; } }; function<void(int&,int)> F[2]={ [](int &x,int y){x=min(x,y);}, [](int &x,int y){x=max(x,y);} }; bool vis[max_n][2]; priority_queue<node> q; inline void dijs(){ for(int i=1;i<=n;++i) f[i][0]=inf,f[i][1]=-inf; for(int i=1;i<=n;++i) if(!deg[i]){ f[i][0]=f[i][1]=0; q.emplace(i,0,0); q.emplace(i,1,0); } while(!q.empty()){ int u=q.top().u,op=q.top().op;q.pop(); if(vis[u][op]) continue; vis[u][op]=1; for(int i=e.hd[u];i;i=e.nx[i]){ int v=e.to[i],w=e.ln[i]; F[op^1](f[v][op^1],f[u][op]+w); if(op || !(--deg[v])) q.emplace(v,op^1,f[v][op^1]); } } }
bool End; #define File "" signed main(){ #ifndef ONLINE_JUDGE freopen(File ".in","r",stdin); freopen(File ".out","w",stdout); #endif debug("Memory : %lf\n",(&Begin-&End)/1024.0/1024); n=read(),m=read(),S=read(); for(int i=1;i<=m;++i){ int u=read(),v=read(),w=read(); e.add(v,u,w); ++deg[u]; } dijs(); if(vis[S][0]) write(f[S][0]); else puts("INFINITY"); return 0; }
|