码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 360E 贪心 最短路

时间:2019-07-04 00:49:19      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:const   include   ons   eof   namespace   print   size   lan   details   

题意及思路:https://blog.csdn.net/huanghongxun/article/details/49846927

在假设所有边都是最大值的情况下,如果第一个人能比第二个人先到,那就缩短这条边。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 10010;
int head[maxn], Next[maxn * 2], ver[maxn * 2], f[maxn * 2], tot;
LL l[maxn * 2], r[maxn * 2];
LL d[2][maxn];
bool v[2][maxn];
queue<int> q[2];
int s1, s2, t, n, m, k;
void add(int x, int y, LL lb, LL rb) {
	ver[++tot] = y, l[tot] = lb, r[tot] = rb, Next[tot] = head[x], head[x] = tot, f[tot] = x;
}
void spfa(int pos, int s) {
//	memset(d[pos], 0x3f, sizeof(d[pos]));
//	memset(v[pos], 0, sizeof(v[pos]));
//	q[pos].push(s), v[pos][s] = 1, d[pos][s] = 0;
	while(q[pos].size()) {
		int x = q[pos].front();
		q[pos].pop();
		v[pos][x] = 0;
		for (int i = head[x]; i; i = Next[i]) {
			int y = ver[i], z = r[i];
			if(d[pos][y] > d[pos][x] + z) {
				d[pos][y] = d[pos][x] + z;
				if(!v[pos][y]) 
					q[pos].push(y), v[pos][y] = 1;
			}
		}
	}
}
bool solve(int flag) {
	memset(d, 0x3f, sizeof(d));
	memset(v, 0, sizeof(v));
	d[0][s1] = d[1][s2] =  0;
	v[0][s1] = v[1][s2] = 1;
	q[0].push(s1), q[1].push(s2);
	bool flag1 = 1;
	while(flag1) {
		spfa(0, s1), spfa(1, s2);
		flag1 = 0;
		for (int i = m + 1; i <= k + m; i++) {
			if(d[0][f[i]]  + flag <= d[1][f[i]] && l[i] != r[i]) {
				flag1 = 1;
				q[0].push(f[i]), q[1].push(f[i]);
				v[0][f[i]] = 1, v[1][f[i]] = 1;
				r[i] = l[i];
			}
		}
	}
	return d[0][t] + flag <= d[1][t];
}
int main() {
	int x, y;
	LL z, t1;
	scanf("%d%d%d", &n, &m, &k);
	scanf("%d%d%d", &s1, &s2, &t);
	for (int i = 1; i <= m; i++) {
		scanf("%d%d%lld", &x, &y, &z);
		add(x, y, z, z);
	}
	for (int i = 1; i <= k; i++) {
		scanf("%d%d%lld%lld", &x, &y, &z, &t1);
		add(x, y, z, t1);
	}
	if(solve(1)) {
		printf("WIN\n");
		for (int i = m + 1; i <= m + k; i++)
			printf("%d ", r[i]);
		printf("\n");
		return 0;
	}
	if(solve(0)) {
		printf("DRAW\n");
		for (int i = m + 1; i <= m + k; i++)
			printf("%d ", r[i]);
		printf("\n");
		return 0;
	}
	printf("LOSE\n");
} 

  

Codeforces 360E 贪心 最短路

标签:const   include   ons   eof   namespace   print   size   lan   details   

原文地址:https://www.cnblogs.com/pkgunboat/p/11129831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!