1087 All Roads Lead to Rome (考察Dijkstra + DFS) 1034 Head of a Gang

 处理 编号和字符串 映射的方式相同于  1034 Head of a Gang

 这是一道模板题,要记住大体流程,然后反复练习。这里我使用的方法是Dijkstra+DFS。

  1 #include<iostream>
  2 #include<vector>
  3 #include<map>
  4 using namespace std;
  5 const int maxn = 230;
  6 const int inf = 0x3fffffff;
  7 
  8 int N,K,st,G[maxn][maxn];
  9 bool visited[maxn];
 10 int d[maxn];
 11 vector<int> pre[maxn],path,tempPath;
 12 int weight[maxn];
 13 
 14 void Dijkstra(int s) {
 15     fill(visited,visited+maxn,false);
 16     fill(d,d+maxn,inf);
 17     d[s] = 0;
 18     for(int i = 0; i < N; ++i) {
 19         int u = -1,MIN = inf;
 20         for(int j = 0; j < N; ++j) {
 21             if(visited[j] == false && d[j] < MIN) {
 22                 u = j;
 23                 MIN = d[j];
 24             }
 25         }
 26         if(u == -1) return ;
 27         visited[u] = true;
 28         for(int v = 0; v < N; ++v) {
 29             if(visited[v] == false && G[u][v] != inf) {
 30                 if(d[u] + G[u][v] < d[v]) {
 31                     d[v] = d[u] + G[u][v];
 32                     pre[v].clear();
 33                     pre[v].push_back(u);
 34                 } else if(d[u]+G[u][v] == d[v])
 35                     pre[v].push_back(u);
 36             }
 37         }
 38     }
 39 }
 40 
 41 map<string,int> stringToint;
 42 map<int,string> intTostring;
 43 int num = 0;
 44 int getID(string str) {
 45     if(stringToint.count(str) == 0) {
 46         stringToint[str] = num;
 47         intTostring[num] = str;
 48         num++;
 49     }
 50     return stringToint[str];
 51 }
 52 
 53 int numPath = 0,maxValue = -1;
 54 double maxAvg = -1;
 55 void DFS(int v) {
 56     if(v == st) {
 57         tempPath.push_back(v);
 58         numPath++;//最短路径条数加 1
 59         int value = 0,avg = 0;
 60         for(int i = tempPath.size()-2; i >= 0; --i)//计算幸福数、点权之和
 61             value += weight[tempPath[i]];
 62         avg = 1.0*value/(tempPath.size()-1);
 63         if(value > maxValue) {
 64             maxValue = value;
 65             maxAvg = avg;
 66             path = tempPath;
 67         } else if(value == maxValue && avg > maxAvg) {
 68             maxAvg = avg;
 69             path = tempPath;
 70         }
 71         tempPath.pop_back();
 72         return ;
 73     }
 74     tempPath.push_back(v);
 75     for(int i = 0; i < pre[v].size(); ++i)
 76         DFS(pre[v][i]);
 77     tempPath.pop_back();
 78 }
 79 int main() {
 80     fill(G[0],G[0]+maxn*maxn,inf);//初始化
 81     string str,str1,str2;
 82     cin>>N>>K>>str;
 83     st = getID(str);
 84     for(int i = 0; i < N-1; ++i) {
 85         cin>>str;
 86         cin>>weight[getID(str)];
 87     }
 88     for(int i = 0; i < K; ++i) {
 89         cin>>str1>>str2;
 90         int u = getID(str1),v = getID(str2);
 91         cin>>G[u][v];
 92         G[v][u] = G[u][v];
 93     }
 94     Dijkstra(st);
 95     DFS(stringToint["ROM"]);
 96     printf("%d %d %d %d\n",numPath,d[stringToint["ROM"]],maxValue,(int)maxAvg);
 97     for(int i = path.size()-1; i >= 0; --i) {
 98         if(i < path.size()-1) printf("->");
 99         cout<<intTostring[path[i]];
100     }
101     return 0;
102 }

猜你喜欢

转载自www.cnblogs.com/keep23456/p/12450486.html