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

SGU 103. Traffic Lights 带限制最短路

时间:2014-09-24 00:30:05      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   on   c   html   amp   时间   

每个点有2中颜色 只有一条路上的两个点颜色一样才能通过这条路 最短路加上等待的时间处理 处理的是参考别人的 唉还是太弱了

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int s, e;
int n, m;
int a[333];
int b[333];
int c[333];

int first[333], cnt;
struct edge
{
	int v, w, next;
}G[30000];


int d[333];
int p[333];
bool vis[333];
struct HeapNode
{
	int v, d;
	HeapNode(){}
	HeapNode(int v, int d): v(v), d(d){}
	bool operator < (const HeapNode& rhs) const
	{
		return d > rhs.d;
	}
};

void AddEdge(int u, int v, int w)
{
	G[cnt].v = v;
	G[cnt].w = w;
	G[cnt].next = first[u];
	first[u] = cnt++;
	
	G[cnt].v = u;
	G[cnt].w = w;
	G[cnt].next = first[v];
	first[v] = cnt++;
	
}

int wait(int t, int x, int y)
{
	int ans = 0;
	int t1 = (a[x] + t) % c[x];
	int t2 = (a[y] + t) % c[y];
	int tmp;
	for(int i = 0; i < 4; i++)
	{
		if((t1 < b[x]) == (t2 < b[y]))
			return ans;
		if(t1 < b[x])
			tmp = b[x] - t1;
		else
			tmp = c[x] - t1;
		if(t2 < b[y])
			tmp = min(tmp, b[y] - t2);
		else
			tmp = min(tmp, c[y] - t2);
		t1 = (t1 + tmp) % c[x];
		t2 = (t2 + tmp) % c[y];
		ans += tmp;
	}
	return -1;
}

void print(int x)
{
	if(p[x] == -1)
	{
		printf("%d", x);
		return;
	}
	print(p[x]);
	printf(" %d", x);
}
void Dijkstra()
{
	for(int i = 1; i <= n; i++)
	{
		d[i] = 999999999;
		p[i] = -1;
		vis[i] = false;
	}
	d[s] = 0;
	priority_queue <HeapNode> Q;
	Q.push(HeapNode(s, 0));
	while(!Q.empty())
	{
		HeapNode x = Q.top(); Q.pop();
		int u = x.v;
		if(vis[u])
			continue;
		vis[u] = 1;
		for(int i = first[u]; i != -1; i = G[i].next)
		{
			int v = G[i].v;
			int x = d[u];
			int t = wait(x, u, v);
			if(t != -1 && d[v] > d[u] + G[i].w + t)
			{
				d[v] = d[u] + G[i].w + t;
				p[v] = u;
				Q.push(HeapNode(v, d[v]));
			}
		}
	}
	if(d[e] != 999999999)
	{
		printf("%d\n", d[e]);
		print(e);
		puts("");
	}
	else
		puts("0");
}
int main()
{
	memset(first, -1, sizeof(first));
	cnt = 0;
	scanf("%d %d", &s, &e);
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++)
	{
		char s[10];
		int r, bl, pu;
		scanf("%s %d %d %d", s, &r, &bl, &pu);
		if(s[0] == 'B')
		{
			a[i] = bl-r;
			b[i] = bl;
			c[i] = bl+pu;
		}
		else
		{
			a[i] = bl+pu-r;
			b[i] = bl;
			c[i] = bl+pu;
		}
	}
	for(int i = 1; i <= m; i++)
	{
		int u, v, w;
		scanf("%d %d %d", &u, &v, &w);
		AddEdge(u, v, w);
	}
	Dijkstra();
	return 0;
}


SGU 103. Traffic Lights 带限制最短路

标签:io   ar   for   sp   on   c   html   amp   时间   

原文地址:http://blog.csdn.net/u011686226/article/details/39507529

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