Code:
#include <queue> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #define N 3004 #define M 70004 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; int edges,n,m; vector<int>G[N]; ll arrive[N],into[N],d[N]; int hd[N],to[M<<1],nex[M<<1],val[M<<1],deg[N],done[N]; void addedge(int u,int v,int c) { nex[++edges]=hd[u],hd[u]=edges,to[edges]=v,val[edges]=c; } struct Node { int u; ll dis; Node(int u=0,ll dis=0):u(u),dis(dis){} bool operator<(Node b) const { return b.dis<dis; } }; priority_queue<Node>q; void Dijkstra() { int s = 1; memset(d,0x3f,sizeof(d)); memset(arrive,0x3f,sizeof(arrive)); d[s]=arrive[s]=into[s]=deg[s]=0, q.push(Node(s,0)); for(;!q.empty();) { Node e=q.top(); q.pop(); int u=e.u; if(done[u]) continue; done[u]=1; for(int i=hd[u];i;i=nex[i]) { int v=to[i]; if(arrive[v]>d[u]+val[i]) { arrive[v]=d[u]+val[i]; if(deg[v]==0) { d[v]=max(into[v], arrive[v]); q.push(Node(v, d[v])); } } } for(int i=0;i<G[u].size();++i) { int v=G[u][i]; --deg[v]; into[v]=max(into[v], d[u]); if(deg[v]==0) { d[v]=max(into[v], arrive[v]); q.push(Node(v, d[v])); } } } } int main() { int i,j; // setIO("input"); scanf("%d%d",&n,&m); for(i=1;i<=m;++i) { int a,b,c; scanf("%d%d%d",&a,&b,&c),addedge(a,b,c); } for(i=1;i<=n;++i) { int t,k; scanf("%d",&t); for(j=1;j<=t;++j) { ++deg[i],scanf("%d",&k),G[k].push_back(i); } } Dijkstra(); printf("%lld\n",d[n]); return 0; }