您的位置:首页 > 其它

UVA 12661 Funny Car Racing

2014-10-09 22:11 405 查看
[b]E - Funny Car Racing[/b]
Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu

There is a funny car racing in a city with n junctions and m directed roads. The funny part is: each road is open and closed periodically. Each road is associate with two integers (a,b), that means the road will be open for a seconds, then closed for b seconds, then open for a seconds... All these start from the beginning of the race. You must enter a road when it’s open, and leave it before it’s closed again. Your goal is to drive from junction s and arrive at junction t as early as possible. Note that you can wait at a junction even if all its adjacent roads are closed.

Input
There will be at most 30 test cases. The first line of each case contains four integers n, m, s, t (1 ≤ n ≤ 300, 1 ≤ m ≤ 50,000, 1 ≤ s,t ≤ n). Each of the next m lines contains five integers u, v, a, b, t (1 ≤ u,v ≤ n, 1 ≤ a,b,t ≤ 105), that means there is a road starting from junction u ending with junction v. It’s open for a seconds, then closed for b seconds (and so on). The time needed to pass this road, by your car, is t. No road connects the same junction, but a pair of junctions could be connected by more than one road.

Output
For each test case, print the shortest time, in seconds. It’s always possible to arrive at t from s.

Sample Input
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

Sample Output
Case 1: 20

Case 2: 9

解题:dijkstra...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 310;
struct arc{
int to,a,b,t,next;
arc(int o = 0,int aa = 0,int bb = 0,int tt = 0,int z = -1){
to = o;
a = aa;
b = bb;
t = tt;
next = z;
}
};
arc e[51000];
int head[maxn],tot,d[maxn],n;
bool vis[maxn];
void add(int u,int v,int a,int b,int t){
e[tot] = arc(v,a,b,t,head[u]);
head[u] = tot++;
}
priority_queue< pii,vector< pii >,greater< pii > >q;
void dijkstra(int s){
for(int i = 0; i <= n; ++i){
d[i] = INF;
vis[i] = false;
}
while(!q.empty()) q.pop();
d[s] = 0;
q.push(make_pair(d[s],s));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next){
int res = d[u]%(e[i].a + e[i].b);
if(e[i].a - res >= e[i].t && d[e[i].to] > d[u] + e[i].t){
d[e[i].to] = d[u] + e[i].t;
q.push(make_pair(d[e[i].to],e[i].to));
}else if(e[i].t <= e[i].a && d[e[i].to] > d[u] + e[i].t + e[i].a + e[i].b - res){
d[e[i].to] = d[u] + e[i].t + e[i].a + e[i].b - res;
q.push(make_pair(d[e[i].to],e[i].to));
}
}
}

}
int main() {
int m,s,t,a,b,u,v,w,cs = 1;
while(~scanf("%d %d %d %d",&n,&m,&s,&t)){
memset(head,-1,sizeof(head));
for(int i = tot = 0; i < m; i++){
scanf("%d %d %d %d %d",&u,&v,&a,&b,&w);
add(u,v,a,b,w);
}
dijkstra(s);
printf("Case %d: %d\n",cs++,d[t]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: