uva 12661 Funny Car Racing - 单源最短路-道路有时间限制
2017-04-25 15:00
447 查看
题意:
从 u -> v 道路开放a秒,关闭b秒,从 u->v 需要花费 t 秒,在点处可以进行等待,等路开放
问从起点到终点最短要多少秒
解题思路
但源最短路,dijkstra就可以解决
首先 t>a 的路可以直接抛弃了
其次到达当前点更新的时候有两种情况,
1、道路是开放的并且可以在规定时间内走完这条路 d[u]%(a+b) + cost<= a
2、道路是不开的 并且等待几秒再走 a+b - d[u]%(a+b) + cost < d[v]
代码:
从 u -> v 道路开放a秒,关闭b秒,从 u->v 需要花费 t 秒,在点处可以进行等待,等路开放
问从起点到终点最短要多少秒
解题思路
但源最短路,dijkstra就可以解决
首先 t>a 的路可以直接抛弃了
其次到达当前点更新的时候有两种情况,
1、道路是开放的并且可以在规定时间内走完这条路 d[u]%(a+b) + cost<= a
2、道路是不开的 并且等待几秒再走 a+b - d[u]%(a+b) + cost < d[v]
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cctype> #include <cstdlib> #include <cmath> #include <string> #include <map> #include <set> #include <queue> #include <vector> #include <stack> #include <cctype> using namespace std; typedef long long LL; const int maxn = 50000+10; const int INF = 0x3f3f3f3f; struct edge{ int to,cost; int a,b; }; typedef pair <int,int> P; vector <edge> G[maxn]; int d[maxn]; int n,m; void dijkstra(int s){ priority_queue<P,vector<P>,greater<P> > que; memset(d,INF,sizeof(d)); d[s] = 0; que.push(P(0,s)); while(!que.empty()){ P p = que.top();que.pop(); int v = p.second; if(d[v] < p.first)continue; for(int i = 0;i<G[v].size();++i){ edge e = G[v][i]; if(d[e.to] <= d[v]+e.cost) continue;///不用更新 ///规定时间内能通过 if(d[v]%(e.a+e.b) + e.cost <= e.a){ d[e.to] = d[v] + e.cost; que.push(P(d[e.to],e.to)); } ///规定时间不能通过,等待一段时间 else if(d[e.to] > d[v]+e.cost + (e.a+e.b) - (d[v]%(e.a+e.b))){ d[e.to] = d[v]+e.cost + (e.a+e.b) - (d[v]%(e.a+e.b)); que.push(P(d[e.to],e.to)); } } } } int main() { int n,m,s,t; int u,v,a,b,c; int cas = 1; while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF){ for(int i = 0;i<maxn;++i){ G[i].clear(); } while(m--){ edge e; scanf("%d%d%d%d%d",&u,&e.to,&e.a,&e.b,&e.cost); if(e.a < e.cost) continue; G[u].push_back(e); } dijkstra(s); printf("Case %d: %d\n",cas++,d[t]); } return 0; } /* 3 2 1 3 1 2 5 6 3 2 3 7 7 6 3 2 1 3 1 2 5 6 3 2 3 9 5 6 */
相关文章推荐
- 最短路 uva12661 Funny Car Racing
- UVA 12661 Funny Car Racing(最短路)
- UVa 12661 (单源最短路) Funny Car Racing
- UVA 12661 Funny Car Racing (Dijkstra最短路)
- UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)
- CSU 1333 & Uva 12661 Funny Car Racing【最短路变形+spfa算法,链式前向星建图】
- UVA 12661 Funny Car Racing(最短路)
- UvA 12661 Funny Car Racing (最短路)
- Funny Car Racing UVA - 12661 --SPFA最短路变形
- UVA 12661 Funny Car Racing(最短路)
- Funny Car Racing UVA - 12661 (当初就不该浪的最短路)
- UVa 12661 Funny Car Racing
- UVA 12661 Funny Car Racing
- UVA-12661 Funny Car Racing (dijkstra)
- UVa 12661 Funny Car Racing - spfa
- 最短路 uva12661 Funny Car Racing
- UVa - 12661 - Funny Car Racing
- uva 12661 - Funny Car Racing
- 12661 - Funny Car Racing (最短路)
- UVa 12661 Funny Car Racing【 dijkstra 】