Pat (practice--1087) (all Roads leads to Rome)

Source: Internet
Author: User

Pat1087 CodeTitle Description:

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

Input Specification:

Each input file contains the one test case. The first line contains 2 positive integers N (2<=n<=200), the number of cities, and K, the total num ber 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 this city, Except the starting city. Then K-lines follow, each describes a route between-cities in the format "City1 City2 cost". The name of a city is a string of 3 capital 中文版 letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we is supposed to find the route with the least cost. If Such a route is isn't unique, the one with the maximum happiness would be recommended. If such a route is still isn't unique, then we output the one with the maximum average happiness--it's 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 on the next line, you were 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 Code:Dijkstra+dfs
#include <cstdio> #include <iostream> #include <string> #include <map> #include <vector># Define MAXN 205#define MAX 0x0fffffffusing 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]);p ath.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);p rintf ("%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;}

Pat (practice--1087) (all Roads leads to Rome)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.