//UVA12661FunnyCarRacing
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 500;
const int INF = 1000000000;
struct Edge {
int from, to, open, close, t;
Edge(int from, int to, int open, int close, int t) : from(from), to(to), open(open), close(close), t(t) {}
};
vector<Edge> edges;
vector<int> G[maxn];
struct HeapNode {
int p, dist;
bool operator < (const HeapNode& rhs) const {
return dist > rhs.dist;
}
};
int dist[maxn], vis[maxn];
void init(int n) {
for(int i = 0; i <= n; i++) {
dist[i] = INF;
G[i].clear();
}
edges.clear();
memset(vis, 0, sizeof(vis));
}
int Dijkstra(int s, int t) {
priority_queue<HeapNode> q;
q.push((HeapNode){s, 0});
dist[s] = 0;
while(!q.empty()) {
//printf("*****\n");
HeapNode tm = q.top(); q.pop();
int x = tm.p;
//printf("x = %d\n", x);
if(vis[x]) continue;
vis[x] = 1;
//dist[x] = tm.dist;
for(int i = 0; i < G[x].size(); i++) {
//printf("G[x].size() = %d\n", G[x].size());
Edge tmp = edges[G[x][i]];
int open, close, from, to, time;
from = tmp.from;
to = tmp.to;
//printf("tmp.open = %d\n", tmp.open);
open = tmp.open;
close = tmp.close;
time = tmp.t;
if(time > open) continue;//无任何通过的可能性
if(dist[from] % (open + close) + time <= open) {
if(dist[to] > dist[from] + time) {
dist[to] = dist[from] + time;//松弛操作
q.push((HeapNode){to, dist[to]});
}
}
else {
int len = dist[from] + open + close + time - dist[from] % (open + close);
if(len < dist[to]) {
// printf("22\n");
dist[to] = len;
q.push((HeapNode){to, dist[to]});
}
}
}
}
return dist[t];
}
int main() {
int n, m, s, t;
int kase = 0;
//freopen("UVA12661out.txt", "w", stdout);
while(scanf("%d%d%d%d", &n, &m, &s, &t) == 4 && n) {
int from, to, open, close, time;
init(n);
for(int i = 0; i < m; i++) {
scanf("%d%d%d%d%d", &from, &to, &open, &close, &time);
edges.push_back(Edge(from, to, open, close, time));
int size = edges.size();
//printf("edges[size - 1].open = %d, close = %d\n", edges[size - 1].open, edges[size - 1].close);
G[from].push_back(size - 1);
}
int ans = Dijkstra(s, t);
printf("Case %d: %d\n", ++kase, ans);
}
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
*/
UVA12661FunnyCarRacing
最新推荐文章于 2019-03-17 16:29:40 发布