PAT 1087

又是最短路,陈越姥姥好像很喜欢最短路... 这道题之后还得再写一遍,最短路一定要熟悉

碰到一个坑,就是在加最短路的时候,不是++,而是要把前面的最短路加进来...

  1 #include <vector>
  2 #include <string>
  3 #include <iostream>
  4 #include <map>
  5 #include <fstream>
  6
  7 //#define OJ
  8
  9 #ifdef OJ
 10 #define fin cin
 11 #endif
 12
 13 using namespace std;
 14
 15 class city{
 16 public:
 17     int happiness;
 18     string name;
 19
 20     vector<int> adj;
 21     vector<int> cost;
 22 };
 23
 24 vector<city> cities;
 25 map<string, int> city_idx;
 26 map<int, string> city_name;
 27
 28 void dij(){
 29     int total = cities.size();
 30     int MAX = 0x7fffffff;
 31
 32     vector<bool> visited(total, false);
 33     vector<int> dij_dist(total, MAX);
 34     vector<int> dij_happiness(total, 0);
 35     vector<int> prev(total, -1);
 36     vector<int> dij_cnt(total, MAX);
 37     vector<int> dij_least_cnt(total, 0);
 38
 39     int target_idx = city_idx["ROM"];
 40
 41     dij_dist[0] = 0;
 42     dij_cnt[0] = 0;
 43     dij_least_cnt[0] = 1;
 44
 45     while (true){
 46         int least_dist = MAX;
 47         int least_idx = -1;
 48
 49         for (int i = 0; i < total; i++){
 50             if (!visited[i] && least_dist > dij_dist[i]){
 51                 least_idx = i;
 52                 least_dist = dij_dist[i];
 53             }
 54         }
 55
 56         if (least_idx == -1)
 57             break;
 58         if (least_idx == target_idx) // ?
 59             break;
 60
 61         visited[least_idx] = true;
 62         vector<int> &adj = cities[least_idx].adj;
 63         vector<int> &cost = cities[least_idx].cost;
 64         for (int i = 0; i < adj.size(); i++){
 65             int cur_idx = adj[i];
 66             if (visited[cur_idx])
 67                 continue;
 68             if (least_dist + cost[i] < dij_dist[cur_idx]){
 69                 dij_dist[cur_idx] = least_dist + cost[i];
 70                 prev[cur_idx] = least_idx;
 71                 dij_cnt[cur_idx] = dij_cnt[least_idx] + 1;
 72                 dij_happiness[cur_idx] = dij_happiness[least_idx] + cities[cur_idx].happiness;
 73                 dij_least_cnt[cur_idx] = dij_least_cnt[least_idx];
 74             }
 75             else if (least_dist + cost[i] == dij_dist[cur_idx]){
 76                 dij_least_cnt[cur_idx] += dij_least_cnt[least_idx];
 77
 78                 if (dij_happiness[least_idx] + cities[cur_idx].happiness > dij_happiness[cur_idx]){
 79                     prev[cur_idx] = least_idx;
 80                     dij_cnt[cur_idx] = dij_cnt[least_idx] + 1;
 81                     dij_happiness[cur_idx] = dij_happiness[least_idx] + cities[cur_idx].happiness;
 82                 }
 83                 else if (dij_happiness[least_idx] + cities[cur_idx].happiness == dij_happiness[cur_idx]){
 84                     if (dij_cnt[least_idx] + 1 < dij_cnt[cur_idx]){
 85                         prev[cur_idx] = least_idx;
 86                         dij_cnt[cur_idx] = dij_cnt[least_idx] + 1;
 87                     }
 88                 }
 89             }
 90         }
 91     }
 92
 93     int idx = target_idx;
 94     cout << dij_least_cnt[target_idx] << " " << dij_dist[target_idx] << " " << dij_happiness[target_idx] << " " << (int)(dij_happiness[target_idx] / dij_cnt[target_idx]) << endl;
 95
 96     vector<int> res;
 97
 98     while (idx != 0){
 99         res.push_back(idx);
100         idx = prev[idx];
101     }
102     cout << city_name[0];
103     for (int i = res.size() - 1; i >= 0; i--)
104         cout << "->" << city_name[res[i]];
105     cout << endl;
106 }
107
108 int main(){
109 #ifndef OJ
110     ifstream fin;
111     fin.open("in.data");
112 #endif
113
114
115     int N, K;
116     fin >> N >> K;
117
118     string name;
119     fin >> name;
120     city_idx[name] = 0;
121     city_name[0] = name;
122
123     city c;
124     c.name = name;
125     c.happiness = 0;
126
127     cities.push_back(c);
128
129     for (int i = 1; i < N; i++){
130         fin >> c.name;
131         fin >> c.happiness;
132
133         cities.push_back(c);
134
135         city_name[i] = c.name;
136         city_idx[c.name] = i;
137     }
138
139     for (int i = 0; i < K; i++){
140         int c1, c2, cost;
141
142         fin >> name;
143         c1 = city_idx[name];
144
145         fin >> name;
146         c2 = city_idx[name];
147
148         fin >> cost;
149
150         cities[c1].adj.push_back(c2);
151         cities[c1].cost.push_back(cost);
152         cities[c2].adj.push_back(c1);
153         cities[c2].cost.push_back(cost);
154     }
155
156     dij();
157
158     return 0;
159 }
时间: 2024-10-25 20:15:58

PAT 1087的相关文章

[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

PAT 1087 有多少不同的值

https://pintia.cn/problem-sets/994805260223102976/problems/1038429191091781632 当自然数 n 依次取 1.2.3.--.N 时,算式 ? 有多少个不同的值?(注:? 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分.) 输入格式: 输入给出一个正整数 N(2). 输出格式: 在一行中输出题面中算式取到的不同值的个数. 输入样例: 2017 输出样例: 1480 代码: #include <bits/stdc

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

pat 乙级题目 1087

题目 1087 有多少不同的值 (20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ?n/2?+?n/3?+?n/5? 有多少个不同的值?(注:?x? 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分.) 输入格式: 输入给出一个正整数 N(2≤N≤10?4??). 输出格式: 在一行中输出题面中算式取到的不同值的个数. 输入样例: 2017 输出样例: 1480 ----------------------------------------------我是华丽的分

PAT basic 1087

1087 有多少不同的值 (20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ?n/2?+?n/3?+?n/5? 有多少个不同的值?(注:?x? 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分.) 输入格式: 输入给出一个正整数 N(2≤N≤10?4??). 输出格式: 在一行中输出题面中算式取到的不同值的个数. 输入样例: 2017 输出样例: 1480 这道题熟练使用STL,很容易做出来. 1 #include<iostream> 2 #include<

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

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甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m