比赛 20110727 评测结果 AAAAAAAAA
题目名称 道路重建 最终得分 100
用户昵称 Citron酱 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2011-07-27 10:32:46
显示代码纯文本
#include <fstream>

#define I_F "rebuild.in"
#define O_F "rebuild.out"
#define MAXn (100+1)

using namespace std;

short n;
short map[MAXn][MAXn];
short a,b;

void Clear();
void Input();
void Floyd();
void Output();

int main()
{
	Clear();
	Input();
	Floyd();
	Output();
	return 0;
}

void Clear()
{
	for (short i=0; i<MAXn; i++)
		for (short j=0; j<MAXn; j++)
			map[i][j]=-1;
}

void Input()
{
	short m,tx,ty;
	ifstream fin(I_F);
	fin>>n>>m;
	for (short i=0; i<m; i++)
	{
		fin>>tx>>ty;
		fin>>map[tx][ty];
		map[ty][tx]=map[tx][ty];
	}
	
	short d;
	bool f[MAXn][MAXn]={{false}};
	fin>>d;
	for (short i=0; i<d; i++)
	{
		fin>>tx>>ty;
		f[tx][ty]=f[ty][tx]=true;
	}
	
	fin>>a>>b;
	fin.close();
	
	for (short i=1; i<=n; i++)
		for (short j=1; j<=n; j++)
			if ((!f[i][j])&&(map[i][j]>0))
				map[i][j]=0;
}

void Floyd()
{
	for (short k=1; k<=n; k++)
		for (short i=1; i<=n; i++)
			for (short j=1; j<=n; j++)
				if ((map[i][k]>=0)&&(map[k][j]>=0)&&((map[i][k]+map[k][j]<map[i][j])||(map[i][j]==-1)))
					map[i][j]=map[i][k]+map[k][j];
}

void Output()
{
	ofstream fout(O_F);
	fout<<map[a][b]<<endl;
	fout.close();
}