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 }

 

posted @ 2019-02-09 16:14  *starry*  阅读(301)  评论(0编辑  收藏  举报