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;
}

转载于:https://www.cnblogs.com/jaxihjk/archive/2011/04/24/2026441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值