1087. All Roads leads to Rome (30) "Shortest Path"--pat (Advanced level) practise

Source: Internet
Author: User

Topic Information

1087. All Roads leads to Rome (30)

Time limit of MS
Memory Limit 65536 KB
Code length limit 16000 B
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 not unique and then we output the one with the maximum average happiness–it are guaranteed by the J Udge 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
Thinking of solving problems
Dijkstra algorithm
AC Code

#include <iostream>#include <stdio.h>#include <string>#include <map>#include <vector>using namespace STD;#define MAX 66666666#define SIZE 203 vector<int>Respath;introutesum=0, shortest = max,reshap=0, Avghap =0; Map<string,int>Hapiness1; map<int,string>Hapiness2; map<int,int>Hapset;intGraph[size][size];intDij[size];intVisit[size];intN,k;voidDfsintStartintEnd vector<int>&path,intHapsum,intPathsum);voidDijkstraints);intMain () {intI,j,index1,tmp1,tmp2,tmp3;intDest =0;stringCity1,city2,city3,city4;Cin>>n>>k>>city1; index1=0;    Hapiness1[city1] = index1;    HAPINESS2[INDEX1] = city1; HAPSET[INDEX1] =0; for(i=1; i<n;i++) {Cin>>city2>>tmp1;if(City2.compare ("ROM")==0) {dest = i;        } Hapiness1[city2] = i;        Hapiness2[i] = City2;    Hapset[i] = TMP1; } for(i=0; i<n;i++) {dij[i] = MAX; for(j=0; j<n;j++) {graph[i][j] = MAX; }    } for(i=0; i<k;i++) {Cin>>city2>>city3>>tmp1;        TMP2 = Hapiness1[city2]; Tmp3 = Hapiness1[city3];if(Tmp1<graph[tmp2][tmp3])            {Graph[tmp2][tmp3] = TMP1;        GRAPH[TMP3][TMP2] = TMP1; }} Dijkstra (0); for(i=0; i<n;i++) {Visit[i] =0; } shortest = dij[dest]; vector<int>MyPath (1,0); visit[0] =1; Dfs0, Dest,mypath,0,0);printf("%d%d%d%d\n", Routesum,shortest,reshap,avghap);cout<<city1; for(i=1; I<respath.size (); i++) {cout<<" ,"<cout<<endl;return 0;}voidDfsintStartintEnd vector<int>&path,intHapsum,intPathsum) {intI,j;if(pathsum>shortest) {return; }if(Start==end) {if(pathsum>shortest) {return; } routesum + +;if(HAPSUM&LT;RESHAP) {return; }intTa = hapsum/(path.size ()-1);if(TA&GT;AVGHAP)            {Respath = path;            Avghap = TA;        Reshap = Hapsum; }return; } for(i=0; i<n;i++) {if(!visit[i] && Graph[start][i]!=max)            {Path.push_back (i); Visit[i] =1;            DFS (I,end,path,hapsum + hapset[i],pathsum+graph[start][i]);            Path.pop_back (); Visit[i] =0; }    }}voidDijkstraints) {intI,j,tmp,so; for(i=0; i<n;i++) {dij[i] = Graph[s][i]; Visit[i] =0; } Dij[s] =0; Visit[s] =1; for(i=1; i<n;i++) {tmp = MAX; so = s; for(j=0; j<n;j++) {if(!visit[j] && dij[j]<tmp)                {tmp = Dij[j];            so = j; }} Visit[so] =1; for(j=0; j<n;j++) {if(!visit[j] && dij[so]+graph[so][j]<dij[j])            {Dij[j] = dij[so]+graph[so][j]; }        }    }}

1087. All Roads leads to Rome (30) "Shortest Path"--pat (Advanced level) practise

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.