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
| #include<bits/stdc++.h> using namespace std; inline int read(){ int res=0,f=1;char ch=getchar(); while(!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar(); while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch&15),ch=getchar(); return res*f; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } int n,m,in[3010],fir[3010],nxt[70010],son[70010],w[70010],tot,vis[3010],t[3010],g[3010],d[3010]; vector<int> v[3010]; inline void add(int x,int y,int z){++tot;nxt[tot]=fir[x];fir[x]=tot;son[tot]=y;w[tot]=z;} priority_queue<pair<int,int> > q; inline void Dij(){ while(!q.empty()) q.pop(); memset(d,63,sizeof(d)); memset(t,63,sizeof(t)); in[1]=g[1]=t[1]=d[1]=0; q.push(make_pair(0,1)); while(!q.empty()){ pair<int,int> u=q.top();q.pop(); if(vis[u.second]) continue ; vis[u.second]=1; for(int to,i=fir[u.second];i;i=nxt[i]){ to=son[i]; if(t[to]>d[u.second]+w[i]){ t[to]=d[u.second]+w[i]; if(!in[to]){ d[to]=max(g[to],t[to]); q.push(make_pair(-d[to],to)); } } } for(auto i:v[u.second]){ g[i]=max(g[i],d[u.second]); in[i]--; if(!in[i]){ d[i]=max(g[i],t[i]); q.push(make_pair(-d[i],i)); } } } } int main(){ n=read(),m=read(); for(int x,y,z,i=1;i<=m;i++) x=read(),y=read(),z=read(),add(x,y,z); for(int x,i=1;i<=n;i++){ x=read(),in[i]=x; for(int y,j=1;j<=x;j++) y=read(),v[y].push_back(i); } Dij(); return write(d[n]),0; }
|