[pat]1087. All Roads leads to Rome (30)

Source: Internet
Author: User

/**************************************************************1087. All Roads leads to Rome (30) time limit MS Memory limit 65536 KB code length limit 16000 B procedure standard author Chen, yueindeed there is many different touri St 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 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 hzhrom 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN rom 1HZH PKN 1PRS rom 2BLN hzh 2PKN GDN 1HZH PRS 1Sample O Utput:3 3 195 97hzh->prs->rom****************************************************************/#include < iostream> #include <map> #include <string> #define INF 1<<30using namespace Std;int mat[210][210]; int dist[210]; bool mark[210];//record happy value int happiness[210];//record total happy value int sumhap[210];//record average happy value int Avg[210];int rout[210];// Record how many shortest path int difr[210];//record path length int ct[210];string cities[210];map<string,int> m;int num=-1;int n;int ston ( string s) {if (M.find (s)!=m.end ()) return M[s];else{num++;m[s]=num;return num;}} void Init () {for (int i=0;i<n;i++) {dist[i]=inf;mark[i]=false;happiness[i]=0;sumhap[i]=0;difr[i]=0;avg[i]=0;rout [I]=0;ct[i]=0;for (int j=0;j<n;j++) {mat[i][j]=inf;}}} void Dijk () {for (int i=0;i<n;i++) {int p;//selects the shortest join path int min=inf;for (int j=0;j<n;j++) if (mark[j]==false&& Dist[j]<min) min=dist[p=j];if (min==inf) return;mark[p]=true;for (int j=0;j<n;j++) {if (mark[j]==true) continue; if (Dist[j]>dist[p]+mat[p][j]) {DIST[J]=DIST[P]+MAT[P][J];ROUT[J]=P;SUMHAP[J]=SUMHAP[P]+HAPPINESS[J];CT[J]=CT [P]+1;avg[j]=sumhap[j]/ct[j];d ifr[j]=difr[p];} else if (Dist[j]==dist[p]+mat[p][j]) {difr[j]+=difr[p];if (Sumhap[j]<sumhap[p]+happiness[j]) {rout[j]=p;sumhap[j  ]=sumhap[p]+happiness[j];ct[j]=ct[p]+1;  AVG[J]=SUMHAP[J]/CT[J];} else if (Sumhap[j]==sumhap[p]+happiness[j]) {if (avg[j]<sumhap[j]/(ct[p]+1)) {rout[j]=p;ct[j]=ct[p]+1;avg[j]= SUMHAP[J]/CT[J];}}}}} void printfrout (int x) {if (rout[x]!=0) printfrout (rout[x]);cout<<cities[rout[x]]<< "--";} int main () {int k;string scity;string city;int hp;string sa,sb;int ia,ib,cost;while (cin>>n>>k>>scity {init (); Num=ston (scity); cities[num]=scity;for (int i=0;i<n-1;i++) {Cin>>city>>hp;num=ston (city); happiness[num]=hp;cities[num]=city;} for (int i=0;i<k;i++) {Cin>>sa>>sb>>cost;ia=ston (SA); Ib=ston (SB); mat[ia][ib]=mat[ib][ia]= Cost;} Dist[0]=0;difr[0]=1;dijk (); Ib=ston ("ROM");cout<<difr[ib]<< "<<dist[ib]<<" "<< sumhap[ib]<< "" <<avg[ib]<<endl;printfrout (IB);cout<< "ROM" <<endl;}} /************************************************************** Author:ws language:c++************************** **************************************/

[pat]1087. All Roads leads to Rome (30)

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.