first flood

/*
	Discription: There is a graph,on which has both directed edges and indirected edges.
						The goal is to calculate the sum of the minimum distance between source
						vertex to 'broken vertex' and 'broken vertex' to source vertex.

		Solution: Build the graph,with the help of the Algorithm called 'Floyd', you can 'relax'
						the paths,and then you've got the shortest paths. Since this kind of angorithm 
						has just three currences,the complexity of the algorithm is just 0(n^3),in which 
						the 'n' is the vertex number in the graph.

						Here is the Detial of Floyd:
							Init:
									for i: 1 to n
										for j: 1 to n
											if i == j : dist[i][j]=0;
											else : dist[i][j]=Infinity;
							Relax(vertex i,vertex j,vertex k):
									if dist[i][j]>dist[i][k]+dist[j][k] : dist[i][j]=dist[i][k]+dist[j][k];
							Folyd:
									for k: 1 to n
										for i: i to n
											for j: 1 to n
												if both three vertexes are different : relax(i,j,k);

						And what you should take care about is the Order of The Three Currences, 
						or you will get the Wrong Answer again and again ! ! !

						As to how to convert the name of city to number,I strongly recommend 'Map'
						in STL (Standard Code Library),which just take O(log(n)) complexity of the 
						algorithm,in which 'n' is the length of the string.

			Tricks: There may be multiple edge values between the same vertexes.You should 
						choose the one with the minimum value.
*/

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <algorithm>
#include <string>
#include <map>

#define N 105
#define inf 0x3f3f3f
#define rep(i,st,ed) for(int i=st;i<=ed;++i)
#define crep(i,st,ed) for(int i=st;i>=ed;--i)
#define clr(x,y) memset(x,y,sizeof(x))

using namespace std;

int n,c,r,num,ans;
int dist[N][N];
int broke[N*10];
map <string,int>  mp;

void init()
{
	ans=num=0;
	mp.clear();
	clr(broke,0);
	rep(i,1,n)
		rep( j,1,n)
		{
			if(i==j) dist[i][j]=0;
			else dist[i][j]=inf;
		}
}

void floyd(int n)
{
	rep(k,1,n)
		rep(i,1,n)
			rep(j,1,n)
				if(i!=j && i!=k && j!=k)
					if(dist[i][k]!=inf &&dist[k][j]!=inf)
						if(dist[i][j]>dist[i][k]+dist[k][j])
							dist[i][j]=dist[i][k]+dist[k][j];
}

void debug()
{
	rep(i,1,n)
	{
		rep(j,1,n) cout<<dist[i][j]<<"\t";
		cout<<endl;
	}
}

int main ()
{
	char src[100],st[100],ed[100],buff[100];
	int a,b,lenth,cas=1;
	while(cin>>n>>c>>r)
	{
		if(!n && !c && !r) break;
		init();
		cin>>src;
		mp[src]=++num;
		int k=0;
		rep(i,1,c)
		{
			cin>>buff;
			if(mp[buff]==0) mp[buff]=++num;
			broke[++k]=mp[buff];
		}
		rep(i,1,r)
		{
			cin>>st>>buff>>ed;
			if(mp[st]==0) mp[st]=++num;
			if(mp[ed]==0) mp[ed]=++num;
			a=mp[st]; b=mp[ed];
			sscanf(buff+2,"%d",&lenth);
			if(buff[0]=='<')
				if(dist[b][a]>lenth) dist[b][a]=lenth;
			if(buff[strlen(buff)-1]=='>')
				if(dist[a][b]>lenth) dist[a][b]=lenth;
		}
		//debug();
		floyd(n);
		rep(i,1,c)
			ans+=dist[1][broke[i]]+dist[broke[i]][1];
		printf("%d. %d\n",cas++,ans);
	}
	system("pause");
	return 0;
}

posted on 2011-04-24 20:37  jaxi  阅读(241)  评论(0编辑  收藏  举报