PAT甲级 All Roads Lead to Rome (dijkstra+dfs回溯)

All Roads Lead to Rome

本题需要记录一共有几条最短路径,并输出最短路中开心值最大的路径或者开心值相等的情况下输出平均开心值最大的路径。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #include <vector>
  6 #include <map>
  7 #include <algorithm>
  8 #define inf 0x3f3f3f3f
  9 using namespace std;
 10 const int maxn=220;
 11 int n,m;
 12 string s1,s2,st;
 13 int cnt;
 14 int fhappy=0;
 15 double fhave=0;
 16
 17 map<string,int>mp1;
 18 map<int,string>mp2;
 19 struct node{
 20     int pos;
 21     int cost;
 22     node(){}
 23     node(int pos,int cost):pos(pos),cost(cost){}
 24     friend bool operator < (node a,node b)
 25     {
 26         return a.cost>b.cost;
 27     }
 28 }head,tail;
 29 vector<node>g[maxn];
 30 vector<int> pre[maxn];
 31 vector<int> path;
 32 vector<int> tpath;
 33 int vis[maxn],dis[maxn];
 34 int happy[maxn];
 35 void dijkstra(int st)
 36 {
 37     priority_queue<node> q;
 38     dis[st]=0;
 39     head.pos=st;
 40     head.cost=0;
 41     q.push(head);
 42     while(!q.empty())
 43     {
 44         head=q.top();
 45         q.pop();
 46         if(vis[head.pos]) continue;
 47         vis[head.pos]=1;
 48         int now=head.pos;
 49         for(int i=0;i<g[now].size();i++)
 50         {
 51             tail=g[now][i];
 52             int v=tail.pos;
 53             int len=tail.cost;
 54             if(dis[v]>dis[head.pos]+len)
 55             {
 56                 dis[v]=dis[head.pos]+len;
 57                 pre[v].clear();
 58                 pre[v].push_back(head.pos);
 59                 q.push(tail);
 60             }
 61             else if(dis[v]==dis[head.pos]+len)
 62             {
 63                 pre[v].push_back(head.pos);
 64                 q.push(tail);
 65             }
 66         }
 67     }
 68 }
 69 void dfs(int now)
 70 {
 71     if(now==0)
 72     {
 73         cnt++;
 74         tpath.push_back(now);
 75         int hval=0;
 76         for(int i=tpath.size()-2;i>=0;i--)
 77         {
 78         //    cout<<"lala"<<tpath[i]<<endl;
 79             hval+=happy[tpath[i]];
 80         }
 81         double have=1.0*hval/(tpath.size()-1);
 82         if(hval>fhappy)
 83         {
 84             fhappy=hval;
 85             fhave=have;
 86             path=tpath;
 87         }
 88         else if(hval==fhappy&&have>fhave)
 89         {
 90             fhappy=hval;
 91             fhave=have;
 92             path=tpath;
 93         }
 94         tpath.pop_back();
 95         return;
 96     }
 97     tpath.push_back(now);
 98     for(int i=0;i<pre[now].size();i++)
 99     {
100         dfs(pre[now][i]);
101     }
102     tpath.pop_back();
103 }
104 int main()
105 {
106     cin>>n>>m>>st;
107     memset(dis,inf,sizeof(dis));
108     mp1[st]=0;
109     mp2[0]=st;
110     int t;
111     for(int i=1;i<=n-1;i++)
112     {
113         cin>>s1>>happy[i];
114         mp1[s1]=i;
115         mp2[i]=s1;
116     }
117     for(int i=0;i<m;i++)
118     {
119         cin>>s1>>s2>>t;
120         int id1=mp1[s1];
121         int id2=mp1[s2];
122         node tmp;
123         tmp.pos=id2;
124         tmp.cost=t;
125         g[id1].push_back(tmp);
126         tmp.pos=id1;
127         g[id2].push_back(tmp);
128     }
129     int ed=mp1["ROM"];
130     dijkstra(0);
131     dfs(ed);
132     printf("%d %d %d %d\n",cnt,dis[ed],fhappy,(int)fhave);
133     for(int i=path.size()-1;i>=0;i--)
134     {
135         cout<<mp2[path[i]];
136         if(i!=0)
137         {
138             printf("->");
139         }
140     }
141     return 0;
142 }

原文地址:https://www.cnblogs.com/1013star/p/10357601.html

时间: 2024-08-01 12:40:57

PAT甲级 All Roads Lead to Rome (dijkstra+dfs回溯)的相关文章

[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 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

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

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

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. F

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 (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

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

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, th