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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include<bits/stdc++.h> #define ll long long #define back return #define ri register int #define ull unsigned ll using namespace std; ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } back x*f; } ll n,m,l[5005],dis[5005],dsy[5005]; bool vis[5005]; vector<ll> a[5005],w[5005],p[5005]; struct node { ll x,tim; node(ll x_,ll tim_) { x=x_; tim=tim_; } friend bool operator < (node a,node b) { back a.tim>b.tim; } }; priority_queue<node> q; void dijkstra(int s) { q.push(node(s,0)),dis[s]=0; while(!q.empty()) { int now=q.top().x; q.pop(); if(vis[now]) continue; vis[now]=1; for(ri i=0;i<a[now].size();i++) { ll to=a[now][i],val=w[now][i]; if(dis[to]>max(dis[now],dsy[now])+val) { dis[to]=max(dis[now],dsy[now])+val; if(dsy[to]==1e17) continue; q.push(node(to,max(dis[to],dsy[to]))); } } for(ri i=0;i<p[now].size();i++) { int to=p[now][i]; l[to]--; if(l[to]==0) { dsy[to]=max(dis[now],dsy[now]); q.push(node(to,max(dis[to],dsy[to]))); } } } } int main() { n=read(),m=read(); for(ri i=1;i<=m;i++) { int x=read(),y=read(),w1=read(); if(x==y) continue; a[x].push_back(y); w[x].push_back(w1); } for(ri i=1;i<=n;i++) { l[i]=read(); if(l[i]==0) dsy[i]=0; else dsy[i]=1e17; for(ri j=1;j<=l[i];j++) { int x=read(); p[x].push_back(i); } } for(ri i=1;i<=n;i++) dis[i]=1e17; dijkstra(1); cout<<max(dsy[n],dis[n])<<"\n"; back 0; }
|