题意:在一个赛车比赛中,有n个交叉点和m条单向道路,每条路都是周期性关闭的,求从S到T的最短路
思路:本题是明显的最短路,不过不同的是花费的总时间并不是经过每条边的通过时间之和,还要加上在每个点等待的总时间。分情况讨论一下即可
#include<bits/stdc++.h> using namespace std; const int maxn = 500; const int INF = 1e9; struct Edge { int from,to,a,b,dist; Edge(int u,int v,int a,int b,int d):from(u),to(v),a(a),b(b),dist(d){} }; vector<Edge>edges; vector<int>G[maxn]; bool inq[maxn]; int n,m,s,t,d[maxn]; void init() { for (int i = 0;i<=n;i++) G[i].clear(); edges.clear(); } void AddEdge(int u,int v,int a,int b,int w) { edges.push_back(Edge(u,v,a,b,w)); int mm = edges.size(); G[u].push_back(mm-1); } void spfa() { memset(inq,0,sizeof(inq)); for (int i = 0;i<n;i++) d[i]=INF; queue<int> q; d[s]=0; inq[s]=1; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); inq[u]=0; for (int i = 0;i<G[u].size();i++) { Edge &e = edges[G[u][i]]; //int v = e.to; int a = e.a,b = e.b; if (a<e.dist) continue; int now = d[u]%(a+b); if (now + e.dist<=a) { if (d[e.to]>d[u]+e.dist) { d[e.to] = d[u]+e.dist; q.push(e.to); inq[e.to]=1; } } else { int wait = a+b-now; if (d[e.to]>d[u]+wait+e.dist) { d[e.to] = d[u]+wait+e.dist; q.push(e.to); inq[e.to]=1; } } } } } int main() { int cas = 1; while (scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF) { init(); s--,t--; for (int i = 0;i<m;i++) { int u,v,a,b,t; scanf("%d%d%d%d%d",&u,&v,&a,&b,&t); AddEdge(u-1,v-1,a,b,t); } spfa(); printf("Case %d: %d\n",cas++,d[t]); } }