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;

}

上一篇:Java基础_04 | 正式进入Java的世界(类和对象、类和对象的内存分配详解、构造函数)


下一篇:Java 多态 polymorphic 枚举 练习