您的位置:首页 > 其它

PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS

2015-07-30 20:32 393 查看
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

用两个map进行城市名与id(0到N-1)的相互映射。幸福指数这个变量不参与求最短路径过程中的修正。可直接用一个map进行id到happy的映射,便于最后的方案比较。
用Dijkstra算法确定ROM到起点的最短路径,在此过程中使用一个前驱数组记录结点前驱。
用前驱数组DFS出所有路径并存在结果数组中,该数组存储每个结点的id。
对所有最短路径通过结点id获取幸福指数,进行方案比较。总幸福指数一样时途径城市少的路径更优。
/*2015.7.30cyq*/
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

//ifstream fin("case1.txt");
//#define cin fin

const int MAX=2147483647;
struct city{
int num;
int length;
city(int n,int l):num(n),length(l){}
};
bool cmp(const city &a,const city &b){
return a.length<b.length;
}
//从ROM开始DFS出所有最短路径,存在result中
void dfs(vector<vector<int> > &preCity,int &start,int &end,vector<int> &path,vector<vector<int> > &result){
path.push_back(start);
if(start==end){
result.push_back(path);
}
else{
for(auto it=preCity[start].begin();it!=preCity[start].end();++it)
dfs(preCity,*it,end,path,result);
}
path.pop_back();
}
int main(){
int N,K;
string s;
cin>>N>>K>>s;
vector<city> AB,UD;//AB为确定集合,UD为未确定集合
vector<vector<int> > roads(N,vector<int>(N,0));
map<int,string> id2str;	//城市名与id相互映射
map<string,int> str2id;
map<int,int> id2happy;
id2happy[0]=0;
str2id[s]=0;
id2str[0]=s;
city cur(0,0);//起点城市
AB.push_back(cur);
int ha;
int D;//终点ROM的id
for(int i=1;i<=N-1;i++){
cin>>s>>ha;
if(s=="ROM")
D=i;
str2id[s]=i;
id2str[i]=s;
id2happy[i]=ha;
UD.push_back(city(i,MAX));//到起点距离为MAX
}
string s1,s2;
int a,b,c;
for(int i=0;i<K;i++){
cin>>s1>>s2>>c;
a=str2id[s1];
b=str2id[s2];
roads[a][b]=c;
roads[b][a]=c;
}
//Dijkstar
vector<vector<int> > preCity(N);//前驱城市
while(cur.num!=D){
for(auto it=UD.begin();it!=UD.end();it++){
if(roads[cur.num][(*it).num]>0){//有路
int tmp=cur.length+roads[cur.num][(*it).num];
if(tmp<(*it).length){
(*it).length=tmp;
preCity[(*it).num].clear();
preCity[(*it).num].push_back(cur.num);
}else if(tmp==(*it).length){
preCity[(*it).num].push_back(cur.num);
}
}
}
sort(UD.begin(),UD.end(),cmp);
cur=UD[0];
UD.erase(UD.begin());
AB.push_back(cur);
}

//用前驱数组dfs出ROM到起点的所有最短路径,存储在result中
vector<int> path;
vector<vector<int> > result;
int start=D;
int end=0;
dfs(preCity,start,end,path,result);

//方案比较
int COST=cur.length;//总费用
int BEST=-1;//最好路线序号,i表示resule[i]
int MHP=0;  //最大幸福指数
int COUNT=0;//路径上的城市数
int mhp,count;
for(int it=0;it<result.size();it++){
mhp=0;
count=0;
for(auto ik=result[it].begin();ik!=result[it].end();ik++){
count++;
mhp+=id2happy[*ik];
}
if(mhp>MHP){
MHP=mhp;
BEST=it;
COUNT=count;
}else if(mhp==MHP){//总幸福相等时
if(count<COUNT){//途径城市少,平均幸福指数就高
BEST=it;
COUNT=count;
}
}
}

cout<<result.size()<<" "<<COST<<" "<<MHP<<" "<<MHP/(COUNT-1)<<endl;
int len=result[BEST].size();
cout<<id2str[result[BEST][len-1]];
for(int i=len-2;i>=0;i--)
cout<<"->"<<id2str[result[BEST][i]];
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: