Abner

博观而约取,厚积而薄发,不可择焉不精,语焉不详!

poj 1122 FDNY to the Rescue!

关键是建图,zoj也有一题,不过格式要求不一样,数据好像也不一样,卡那儿了!

下面是能过poj的代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

const int MAXN = 25;
const int INF = 10000010;

struct Dist
{
	bool station;
	int num;
	int T;
	Dist()
	{
		station = false;
		num = 0;
		T = 0;
	}
}dist[MAXN];

int Edge[MAXN][MAXN];
int path[MAXN];
int S[MAXN];
int n;

int cmp(Dist a, Dist b)
{
	return a.T < b.T;
}

void Dijkstra(int v0)
{
	int i, j;
	for (i = 0; i < n; ++i)
	{
		dist[i].T = Edge[i][v0];
		S[i] = 0;
		if(Edge[i][v0] < INF && i != v0)
			path[i] = v0;
		else
			path[i] = -1;
	}
	S[v0] = 0;
	for (i = 1; i < n; ++i)
	{
		int MIN = INF, v;
		for (j = 0; j < n; ++j)
		{
			if(!S[j] && MIN > dist[j].T)
			{
				MIN = dist[j].T;
				v = j;
			}
		}
		S[v] = 1;
		for (j = 0; j < n; ++j)
		{
			if (!S[j] && Edge[j][v] < INF && dist[j].T > dist[v].T + Edge[j][v])
			{
				dist[j].T = dist[v].T + Edge[j][v];
				path[j] = v;
			}
		}
	}
}

void OutPut(int u)
{
	cout<<u+1<<"\t";
	if(path[u] != -1)
		OutPut( path[u] );
}

int main()
{
	//int T;
	int i, j;
	cin>>n;
	for (i = 0; i < n; ++i)
	{
		for (j = 0; j < n; ++j)
		{
			cin>>Edge[i][j];
			if (Edge[i][j] == -1)
			{
				Edge[i][j] = INF;
			}
		}
	}
	int v0;
	cin>>v0;
	char s[1010];
	gets(s);
	int len = strlen(s);
	for (i = 0; i < len; ++i)
	{
		int t = s[i] - '0';
		dist[t-1].station = true;
		dist[t-1].num = t-1;
	}
	Dijkstra(v0-1);
	sort(dist, dist+n, cmp);
	cout<<"Org\tDest\tTime\tPath"<<endl;
	for (i = 0; i < n; ++i)
	{
		if (dist[i].station)
		{
			cout<<dist[i].num+1<<"\t"<<v0<<"\t"<<dist[i].T<<"\t";
			OutPut(dist[i].num);
			cout<<endl;
	}
}
	//system("pause");
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,转载需注明出处。 https://blog.csdn.net/Hearthougan/article/details/17140523
文章标签: 最短路径
个人分类: 最短路径
上一篇zoj 2750 Idiomatic Phrases Game
下一篇hdu 1432 Lining Up
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭