Travel Cost(迪杰斯特拉)

 
 
 
 

Description

AC country is a famous and beautiful place. There are N cities in the country, numbered as 1,2,3...N。The first city is the capital of AC, so it is the greatest and best place all over the country. Many travelers often get a travel in AC.  ACMer and his girl friends like the fine view and beautiful scenery in AC, so they plan to get a travel in AC recently. AC and his N-1 girl friends go to the AC by plane  land at the capital airport. They wants to get a travel to every city of AC but independently. in other words, the first girl friend goes to the first city, the second girl friend goes to the second city,the rest can be done in the same manner. In AC country, there maybe at most one road between every two different cities. One girl will spend some money if  she passes one road correspondingly. ACMer must offer all the travel cost of all his girl friends. He wants to known what is the minimum total cost if evry girl gets her travel by the most economic strategy ?

Input

There are many test case waiting for your program code! In every case, the first line of the input will be N (1<=N<=100), the number of cites. The rest of the input defines an N*N adjacency matrix. Each of its entries will be either an integer or the character x. The value of MAT(i,j) indicates the expense of sending a travel  directly from city i to city j. A value of x for MAT(i,j) indicates that there is no directly road from city to city j.  Note that for a city to itself does not require any cost, so MAT(i,i) = 0 for all integer i in range(1,N). Also, you may assume that the road is undirected, so that MAT(i,j) = MAT(j,i). Thus the imput data only supply the entries on the lower triangular portion of adjacency matrix strictly.  The input of your program will be the lower triangular section of matrix . That is, the second line of input will contain only, MAT(2,1). The next line will contain two entries, MAT(3,1) and MAT(3,2), and so on. All MAT(i,j) will not greater than 1000.

Output

for every case, output the minimum total cost of all ACMer's all girl friends,  one per line.

Sample Input

5
40
30 6
99 24 36
25 x x 25

Sample Output

141

大致问的就是一个人的女朋友要去各个城市去逛街各个城市之间有路,并且要花路费问所有女朋友逛完街的最少花费是多少,典型的裸的迪杰斯特拉问题求出1到各个城市的最短路劲,把钱一加就行,不多说上代码(因为很简单,代码注释的很全):

 
 
 
 
# if 01
# include <iostream>
# include <numeric>
# include <algorithm>
# include <functional>
# include <list>
# include <map>
# include <set>
# include <stack>
# include <deque>
# include <queue>
# include <vector>
# include <ctime>
# include <cstdlib>
# include <cmath>
# include <string>
# include <cstring>
# include <iomanip>
# include <queue>

using namespace std;
int const maxn = 105;
int e[maxn][maxn], book[maxn], dis[maxn];
int num[maxn];
const int INF = 0x3f3f3f3f;	// 用inf存储一个我们认为的正无穷值

void init()
{
	for(int i = 0; i < maxn; i++)
	{
		for(int j = 0; j < maxn; j++)
		{
			if(i == j)
			{
				e[i][j] = 0;
			}
			else
			{
				e[i][j] = INF;
			}
		}
	}
	memset(book, 0, sizeof(book));
	memset(dis, 0, sizeof(dis));
}

int main(int argc, char *argv[])
{
	int n;
	while(scanf("%d",&n) != EOF)
	{
		init();
		for(int i = 2; i <= n; i++)
		{
			for(int j = 1; j < i; j++)
			{
				char str[10];
				scanf("%s", &str[0]);
		
				if(str[0] != 'x')
				{
					int k = 0;
				
					for(int l = 0; l < strlen(str); l++)
					{
						k = k * 10 + str[l] - '0';
			
					}
			
					e[i][j] = e[j][i] = k;
				}
				else
				{
					e[i][j] = e[j][i] = INF;
				}
			}
		}

		//初始化dis数组, 1号顶点到各个顶点的路程
		for(int i = 1; i <= n; i++)
		{
			dis[i] = e[1][i];
		}
		//标志着1号顶点已经被用
		book[1] = 1;
		int flag;//标记最近点的下标
		//迪杰斯特拉
		//n个点根据每个的点来进行出度和边的松弛,因为有n个点,n-1就行
		for(int i = 1; i <= n - 1; i++)
		{
			int min = INF;
			//找离1号顶点最近的点
			for(int j = 1; j <= n; j++)
			{
	
				if(book[j] == 0 && dis[j] < min)
				{
	
					min = dis[j];
					flag = j;

				}
			
			}

			book[flag] = 1;//找见最近的点对这个点进行标记表示用过
				
			//对这个边进行松弛操作
			for(int v = 1; v <= n; v++)
			{

				//我这个点要小于INF
				if(e[flag][v] < INF)
				{
					//如果我这个点的距离 > 我最近的这个点+这个点的出度的话就更新值
					if(dis[v] > dis[flag] + e[flag][v])
					{
						dis[v] = dis[flag] + e[flag][v];
					}
				}
			}
			
		}
		
	

		int sum = 0;
		for(int i = 1; i <= n; i++)
		{
			sum += dis[i];
		}
		printf("%d\n", sum);

	}
	return 0;
}
/*
6 9
1 2 1
1 3 12
2 3 9
2 4 3
3 5 5
4 3 4
4 5 13
4 6 15
5 6 4

reuslt:0 1 8 4 13 17
*/
# endif



猜你喜欢

转载自blog.csdn.net/I_O_fly/article/details/80423681