C/C++教程

UVa 12661 - Funny Car Racing (最短路变形)

本文主要是介绍UVa 12661 - Funny Car Racing (最短路变形),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 50010;

int n, m, s, t;

int h[305], cnt = 0;
struct E{
int to, a, b, ti, next;
}e[maxn << 1];
void add(int u, int v, int a, int b, int ti){
e[++cnt].to = v;
e[cnt].a = a;
e[cnt].b = b;
e[cnt].ti = ti;
e[cnt].next = h[u];
h[u] = cnt;
}

int d[maxn];
void dij(int s){
priority_queue<pii, vector, greater > q;
memset(d, 0x3f, sizeof(d));
d[s] = 0;
q.push(make_pair(d[s], s));

while(!q.empty()){
	pii p = q.top(); q.pop();
	int u = p.second;
	
	if(p.first > d[u]) continue;
	for(int i = h[u] ; i != -1 ; i = e[i].next){
		int v = e[i].to;
		
		int no = d[u] % (e[i].a + e[i].b);
		
		if(no <= e[i].a && (e[i].a - no) >= e[i].ti){ // 能通过
			if(d[v] > d[u] + e[i].ti){
				d[v] = d[u] + e[i].ti;
				q.push(make_pair(d[v], v));
			}
		} else{ // 等待
			int wait = (e[i].a + e[i].b) - no;
			if(d[v] > d[u] + e[i].ti + wait){
				d[v] = d[u] + e[i].ti + wait;
				q.push(make_pair(d[v], v));
			}
		}
	}
}

}

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
int kase = 0;
while(scanf("%d%d%d%d", &n, &m, &s, &t) != EOF){
memset(h, -1, sizeof(h));
cnt = 0;

	int u, v, a, b, ti;
	for(int i = 1 ; i <= m ; ++i){
		scanf("%d%d%d%d%d", &u, &v, &a, &b, &ti);
		if(ti > a) continue;
		add(u, v, a, b, ti);
	}
	
	dij(s);
	
	printf("Case %d: ", ++kase);
	printf("%d\n", d[t]);
}
return 0;

}


                    
这篇关于UVa 12661 - Funny Car Racing (最短路变形)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!