1087 All Roads Lead to Rome(Dijkstra)

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

AC代码

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#include <string> 
using namespace std; 

const int maxn = 201, INF = 100000;
int happy[maxn]; //快乐度 
string name[maxn]; //城市名 
struct route{
	int v, w; //w表示花费 
	route(int v, int w): v(v), w(w){}
};
map<string, int> mp; //城市名字与其索引
vector<route> G[maxn];
int c[maxn], h[maxn], cnt[maxn]; //花费 快乐度 route数 
int num[maxn]; //成本最低的路径数 
bool vis[maxn] = {false};
int pre[maxn];

void Dijkstra(int s, int n){
	fill(c, c + maxn, INF);
	fill(h, h + maxn, 0);
	fill(cnt, cnt + maxn, INF);
	for(int i = 0; i < n; i++)
		pre[i] = i;
	c[s] = 0;
	cnt[s] = 0;
	num[s] = 1;
	h[s] = happy[s];
	for(int i = 0; i < n; i++){
		int u = -1, min = INF;
		for(int j = 0; j < n; j++){
			if(vis[j] == false && c[j] < min){
				min = c[j];
				u = j;
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int j = 0; j < G[u].size(); j++){
			int v = G[u][j].v;
			if(vis[v] == false){
				if(c[u] + G[u][j].w < c[v]){
					c[v] = c[u] + G[u][j].w;
					h[v] = h[u] + happy[v];
					cnt[v] = cnt[u] + 1;
					pre[v] = u;
					num[v] = num[u];
				}
				else if(c[u] + G[u][j].w == c[v]){
					num[v] += num[u];
					if(h[u] + happy[v] > h[v]){
						h[v] = h[u] + happy[v];
						cnt[v] = cnt[u] + 1;
						pre[v] = u;
					}
					else if(h[u] + happy[v] == h[v]
					&& cnt[u] + 1 < cnt[v]){
						cnt[v] = cnt[u] + 1;
						pre[v] = u;	
					}
				}
			}
		}
	}
}

void DFS(int s, int v){
	if(v == s){
		cout<<name[s];
		return;
	}
	DFS(s, pre[v]);
	cout<<"->"<<name[v];
}

int main(){ 
	int n, k, ed;
	cin>>n>>k;
	cin>>name[0];
	mp[name[0]] = 0;
	happy[0] = 0;
	for(int i = 1; i < n; i++){
		cin>>name[i]>>happy[i];
		if(name[i] == "ROM")
			ed = i;
		mp[name[i]] = i;
	}	
	string a, b;
	int C;
	for(int i = 0; i < k; i++){
		cin>>a>>b>>C;
		G[mp[a]].push_back(route(mp[b], C));
		G[mp[b]].push_back(route(mp[a], C));
	}
	Dijkstra(0, n);
	cout<<num[ed]<<" "<<c[ed]<<" "<<h[ed]<<" "<<h[ed] / cnt[ed]<<endl;
	DFS(0, ed);
	return 0;
}

发布了110 篇原创文章 · 获赞 0 · 访问量 1266

猜你喜欢

转载自blog.csdn.net/qq_43072010/article/details/105449951