比赛 20120710 评测结果 C
题目名称 三元限制最短路 最终得分 0
用户昵称 TBK 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-07-10 10:57:27
显示代码纯文本
/**
*Prob	: patha
*Data	: 2012-7-10
*Sol	: SPFA+Hash
*/

#include <set>
#include <cstdio>
#include <cstring>

#define MaxN 3010
#define MaxE 401000
#define oo 20000000

using namespace std;

//cannot
struct node1 {
	int y,last,next;
} e[MaxE];
int s[MaxN];

//cannot
struct node2 {
	int y,next;
} e[MaxE];

int n,m,k,totk=0,tot=0;
int a[MaxN];
bool v[MaxN][MaxN];
int pre[MaxN][MaxN];
int dis[MaxN][MaxN];
struct {
	int x,l;
}list[200000];

//从x到y不能到z
void insert(int x,int y,int z) {
	e2[totk].y = z; e2[totk].last = x;
	e2[totk].next = s[y]; s[y] = totk; 
}
void insert1(int x,int y) {
	e[tot].y = y;
	e[tot].next = a[x]; a[x] = tot; 
}
//x到y后能不能到c
bool can(int x,int y,int c)
{
	int tmp = s[y];
	for (;tmp;tmp=e2[tmp].next) {
		if (e2[tmp].last==x&&e2[tmp].y==c)
			return false;
	}
	return true;
}

void spfa()
{
	memset(dis,127,sizeof(dis));
	memset(v,false,sizeof(v));
	int open = 1, closd = 0;
	list[1].x = 1; list[1].l = 0;
	dis[0][1] = 0; pre[0][1] = 0;
	v[0][1] = true;
	while (closd<open) {
		int now = list[++closd].x;
		int last = list[closd].l;
		v[last][now] = false;
		int te = a[now];
		for (;te;te=e[te].next) {
			//可以走
			int i = e[te].y;
			if (can(last,now,i)) {
				if (dis[last][now]+1<dis[now][i]) {
					dis[now][i] = dis[last][now]+1;
					pre[now][i] = last;
					if (!v[now][i]) {
						v[now][i] = false;
						list[++open].x = i;
						list[open].l = now;
					}
				}
			}
		}	
	}
}

void print(int x,int y)
{
	if (pre[x][y]!=0) print(pre[x][y],x);
	printf("%d ",x);
}

int main()
{
	freopen("patha.in","r",stdin);
	freopen("patha.out","w",stdout);
	
	scanf("%d%d%d",&n,&m,&k);
	
	int x,y,z;
	for (int i=1; i<=m; i++) {
		scanf("%d%d",&x,&y);
		tot++; insert1(x,y);
		tot++; insert1(y,x);
	}
	for (int i=1; i<=k; i++) {
		scanf("%d%d%d",&x,&y,&z);
		totk++; insert(x,y,z);
	}
	
	spfa();
	
	int ans = oo;
	for (int i=1; i<=n; i++) 
		if (dis[i][n]<ans) {
			k = i;
			ans = dis[i][n];
		}
	printf("%d\n",ans);
	
	print(k,n);
	printf("%d\n",n);
	
	fclose(stdin); fclose(stdout); 
	return 0;
}