Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)

Pat1087代码

题目描述:

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 recommended. 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 recommended 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代码:

dijkstra+DFS

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#define MAXN 205
#define MAX 0x0FFFFFFF

using namespace std;

int n,k;
int matrix[MAXN][MAXN];
string name[MAXN];
int happiness[MAXN];
map<string,int> m;
int dis[MAXN];
int visited[MAXN];
int sumHapp=0,avgHapp=0,routeSum=0,shortest=MAX;
vector<int> resPath;

void dijkstra(int s){
	int i,j,k,temp;
	for(int i=0;i<n;i++){
		dis[i]=matrix[s][i];
		visited[i]=0;
	}
	dis[s]=0;
	visited[s]=1;
	for(int i=1;i<n;i++){
		temp=MAX;
		k=s;
		for(int j=0;j<n;j++){
			if(!visited[j]&&dis[j]<temp){
				temp=dis[j];
				k=j;
			}
		}
		visited[k]=1;
		for(int j=0;j<n;j++){
			if(!visited[j]&&dis[k]+matrix[k][j]<dis[j]){
				dis[j]=dis[k]+matrix[k][j];
			}
		}
	}
}

void DFS(int start,int end,vector<int> &path,int hapSum,int pathSum){
	int i,j;
	if(pathSum>shortest)
		return;
	if(start==end){
		if(pathSum>shortest)
			return;
		routeSum++;
		if(hapSum<sumHapp)
			return;
		int avg=hapSum/(path.size()-1);
		if(avg>avgHapp){
			resPath=path;
			avgHapp=avg;
			sumHapp=hapSum;
		}
		return;
	}
	for(int i=0;i<n;i++){
		if(!visited[i]&&matrix[start][i]!=MAX){
			path.push_back(i);
			visited[i]=1;
			DFS(i,end,path,hapSum+happiness[i],pathSum+matrix[start][i]);
			path.pop_back();
			visited[i]=0;
		}
	}
}

int main(int argc,char *argv[]){
	int dest;
	string city;
	cin>>n>>k>>city;
	name[0]=city;
	m[city]=0;
	happiness[0]=0;
	for(int i=1;i<n;i++){
		cin>>name[i]>>happiness[i];
		if(name[i].compare("ROM")==0){
			dest=i;
		}
		m[name[i]]=i;
	}
	for(int i=0;i<n;i++){
		dis[i]=MAX;
		for(int j=0;j<n;j++){
			matrix[i][j]=MAX;
		}
	}
	string city1,city2;
	int cost;
	for(int i=0;i<k;i++){
		cin>>city1>>city2>>cost;
		int r=m[city1];
		int c=m[city2];
		if(cost<matrix[r][c]){
			matrix[r][c]=matrix[c][r]=cost;
		}
	}
	dijkstra(0);
	for(int i=0;i<n;i++){
		visited[i]=0;
	}
	shortest=dis[dest];
	vector<int> path(1,0);
	visited[0]=1;
	DFS(0,dest,path,0,0);
	printf("%d %d %d %d\n",routeSum,shortest,sumHapp,avgHapp);
	cout<<city;
	for(int i=1;i<resPath.size();i++){
		cout<<"->"<<name[resPath[i]];
	}
	cout<<endl;
	return 0;
}
时间: 2024-08-09 19:51:42

Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)的相关文章

1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise

题目信息 1087. All Roads Lead to Rome (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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. I

[PAT]1087. All Roads Lead to Rome (30)

/************************************************************** 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different tourist routes from our city to Rome. You are suppos

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace st

PAT (Advanced Level) 1087 All Roads Lead to Rome

题解   最短路径经典题型.套最短路的板子再加上额外的要求就可以了(说起来好简单).SPFA也行,Dijkstra也可以.这里我用的是SPFA.因为题目要求,将地名和其对应的数字用map映射一下,这样方便处理. same[i]代表到达地点 i 有几种路径: dist[i]代表从起点到地点 i 的最短距离: happy[i]代表从起点到地点 i 的幸福值: cnt[i]代表从起点到地点 i 需要经过几个城市: ans[i]代表从哪个地点到达了地点 i : 代码 #include<bits/stdc

PAT 1087 All Roads Lead to Rome

1 #include <cstdio> 2 #include <climits> 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <queue> 7 #include <unordered_map> 8 #include <algorithm> 9 10 using namespace std; 11 12 typ

1002 A+B for Polynomials (PAT (Advanced Level) Practice)

This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N?1?? a?N?1???? N?2?? a?N?2?

PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their

PAT (Advanced Level) Practice 1068 Find More Coins

题解 01背包板子 + 记录路径.这次的记录路径比较特殊,要从多组解中找到一组由尽量小价值的硬币组成的解.所以不能利用一维数组记录路径,path[目前重量] = 物品序号,因为这样最后只能记录一个可能符合或不符合要求解.所以应该利用二维数组记录路径,path[ 物品序号 ][ 目前重量 ] = 1,这样可以记录多组解.因为要求为找到最小的一组解,所以先将拥有的硬币从大到小排序,以便于进行01背包时,可以从大到小更新解. 代码 #include<bits/stdc++.h> using name

1087. All Roads Lead to Rome (30)

时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 Spec

pat1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 gain