1087 All Roads Lead to Rome (30 分)(Dij+DFS)

1087 All Roads Lead to Rome (30 分)

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->...->ROM.

Sample Input:

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

感觉数据水了一点吧

dij得到最小的需要花费的 剩下的就去搜索里面了得出那些东西

注意的是输出是cnt , total  刚刚这边写反 了

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int inf = 0x3f3f3f3f;
map<string,int>mp;
map<int,string>mm; //反转 
int maze[205][205];
int vis[205],dis[205],val[205];
int city,rou;
int cnt = 0, maxhappy = 0, total = 0;
vector<int>vec, last;
void DFS(int x, int y/*表示结尾*/, int ans, int happy)
{
	if(ans > total)
		return; //如果当前的钱已经大于total 那么就结束吧 
	if(x == y && ans == total)
	{
		if(happy > maxhappy)
		{
			maxhappy = happy;
			last = vec;
		}else if(happy == maxhappy)
		{
			if(last.size() > vec.size())
				last = vec;
		}
		
		cnt ++;
		return ;
	}	
	for(int i = 1; i <= city; i ++)
	{
		if(maze[x][i] != inf && vis[i] == 0)
		{
			vis[i] = 1;
			vec.push_back(i);
			DFS(i, y,ans + maze[x][i], happy + val[i]);
			vis[i] = 0;
			vec.pop_back();
		}
	}
	return ;
}
int Dij()
{
	int pro = mp["ROM"];
	for(int i = 1; i <= city; i ++)
		dis[i] = maze[1][i];
	vis[1] = 1;//标记下哈哈
	for(int i = 1; i < city; i++)
	{
		int maxn = inf, u = -1;
		for(int j = 1; j <= city; j++)	
		{
			if(vis[j] == 0 && dis[j] < maxn)
			{
				maxn = dis[j];
				u = j;
			}
		}
		if(u == -1)
			break;
		vis[u] = 1;
		for(int j = 1; j <= city; j++ )
		{
			if(vis[j] == 0 )
			{
				if(dis[u] + maze[u][j] < dis[j])
					dis[j] = dis[u] + maze[u][j];	
			}	
		}
	} 
	return dis[pro];
}
int main()
{
	char str[5];
	scanf("%d %d",&city,&rou);
	scanf("%s", str);
	int now = 0;
	mp[str] = ++now;
	mm[now] = str; 
	for(int i = 2; i <= city;i ++)
	{
		scanf("%s %d", str, &val[i]);
		mp[str] = ++now;
		mm[now] = str;
	}
	char a[5], b[5];
	int x, y ,z;
	for(int i = 1 ; i <= city; i ++)
	{
		for(int j = 1; j <= city; j ++)
			maze[i][j] = inf;  
		maze[i][i] = 0;
	} 
	for(int i = 1; i <= rou; i++)
	{
		scanf("%s %s %d", a, b, &z);
		x = mp[a];
		y = mp[b];
		maze[x][y] = maze[y][x] = z;
	}
	total = Dij(); //求出最小需要花费多少钱 然后! 
	memset(vis,0,sizeof(vis));
	vis[1] = 1; 
	vec.push_back(1);
	DFS(1, mp["ROM"], 0 ,0);
	printf("%d %d %d %d\n",cnt,total,maxhappy,(int)(maxhappy/(last.size() - 1)));
	cout<<mm[1];
	for(int i = 1 ; i < last.size(); i++)
		cout<<"->"<<mm[last[i]];
	cout<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/galesaur_wcy/article/details/84259065
今日推荐