fastvj.rainng.com/contest/236779#problem/Inode
Description:
n個點m條路每條路 l,r,t:表示這條路開l秒,關r秒,經過要t秒,問你車輛從s到t最少要多少秒ios
Solution:
(刷着最大流忽然看到了我親愛的最短路,真的是我相見恨晚,並且仍是這個專題的最後一題,嘿嘿嘿,拿下)spa
考點就是spfa的鬆弛操做,dis表示當前到now點的時間,你要判斷如今t秒能不能經過下一條路,能夠的話鬆弛就是dis[now] + t,不行的話你得等,等到那個路通了再走,dis[now] + wait + t;blog
仍是比較年輕,存在有的路開通時間小於經過時間,那些邊不予考慮!ip
Code:string
#include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <algorithm> #define inf (1 << 28) using namespace std; const int maxn = 5e4 + 5; const int maxm = 5e4 + 5e3; struct node{ int to,l,r,t,pre; }e[maxm]; int id[maxn],cnt; //spfa int dis[maxn]; int vis[maxn]; void init() { memset(vis,0,sizeof(vis)); memset(id,-1,sizeof(id)); cnt = 0; } void add(int from,int to,int l,int r,int t) { e[cnt].to = to; e[cnt].l = l; e[cnt].r = r; e[cnt].t = t; e[cnt].pre = id[from]; id[from] = cnt++; } void spfa(int s,int n) { for(int i = 0;i <= n;++i) dis[i] = inf; dis[s] = 0; vis[s] = 1; queue<int> q; q.push(s); while(q.size()) { int now = q.front(); q.pop(); for(int i = id[now];~i;i = e[i].pre) { int to = e[i].to; int t = e[i].t; int l = e[i].l; int r = e[i].r; int tot = l + r; int cost; if((dis[now] % tot) + t <= l)cost = t; else cost = r + (l - (dis[now] % tot)) + t; if(dis[to] > dis[now] + cost) { dis[to] = dis[now] + cost; if(!vis[to]) { vis[to] = 1; q.push(to); } } } vis[now] = 0; } return; } int main() { int S,T,n,m; int cas = 1; while(~scanf("%d%d%d%d",&n,&m,&S,&T)) { init(); int from,to,l,r,t; for(int i = 1;i <= m;++i) { scanf("%d%d%d%d%d",&from,&to,&l,&r,&t); //沒考慮到啊!!!! if(l>=t) add(from,to,l,r,t); } spfa(S,n); printf("Case %d: %d\n",cas++,dis[T]); } return 0; }