The 2019 Asia Nanchang First Round Online Programming Contest B. Fire-Fighting Hero

Topic links: https://nanti.jisuanke.com/t/41349

Meaning of the questions: There is a fire-fighting hero, and a fire-fighting team, a team man and a comparison.

Shortest hero to other fire extinguishing maximum points, with a team of other fire-fighting to the shortest point of maximum distance * C, for comparison.

If one team at a point k, then the shortest point k is 0, so that we can build a virtual point "0" point, run the shortest team

Can let the "0" point to the other team members point to 0, to other points as inf, and then run one of the most short-circuit it, the following code is game time, too lazy to change, it is difficult to see directly wrote.


  1 #include <iostream>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <cstdio>
  5 #include <string>
  6 using namespace std;
  7 
  8 typedef long long LL;
  9 #define inf 1e9
 10 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
 11 #define rep__(i,j,k) for(int i = (j); i < (k); i++)
 12 #define per(i,j,k) for(int i = (j); i >= (k); i--)
 13 #define per__(i,j,k) for(int i = (j); i > (k); i--)
 14 
 15 const int N = 1100;
 16 bool vis[N];
 17 int mp[N][N];
 18 bool fired[N];
 19 int team[N];
 20 int dis[N];
 21 int V,E,S,K,C;
 22 
 23 int dijkstra(int s){
 24 
 25     rep(i,1,V) vis[i] = false;
 26 
 27     if(s){
 28         rep(i,1,V) dis[i] = inf;
 29         rep(i,1,V) dis[i] = mp[s][i];
 30         vis[s] = true;
 31 
 32         rep(i,2,V){//团队的
 33 
 34             int x = -1;
 35             int c = inf;
 36 
 37             rep(j,1,V){
 38                 
 39                 if(!vis[j] && c > dis[j]) x = j, c = dis[j];
 40             }
 41             if(x == -1) continue;
 42 
 43             vis[x] = true;
 44             rep(p,1,V){
 45                 if(!vis[p] && dis[x] + mp[x][p] < dis[p]){
 46                     dis[p] = dis[x] + mp[x][p];
 47                 }
 48             }
 49 
 50         } 
 51     }
 52     else{//英雄的
 53         rep(i,1,V) dis[i] = inf;
 54         rep(i,1,K) dis[team[i]] = 0;
 55         vis[s] = true;
 56 
 57         rep(i,1,V){
 58 
 59             int x = -1;
 60             int c = inf;
 61 
 62             rep(j,1,V){
 63                 
 64                 if(!vis[j] && c > dis[j]) x = j, c = dis[j];
 65             }
 66             if(x == -1) continue;
 67 
 68             vis[x] = true;
 69             rep(p,1,V){
 70                 if(!vis[p] && dis[x] + mp[x][p] < dis[p]){
 71                     dis[p] = dis[x] + mp[x][p];
 72                 }
 73             }
 74 
 75         } 
 76     }
 77 
 78 
 79     int ans = 0;
 80     rep(i,1,V){
 81         
 82         if(dis[i] == inf) continue;
 83         if(fired[i]) ans = max(ans,dis[i]);
 84     }
 85 
 86     return ans;
 87 }
 88 
 89 int main(){
 90 
 91     ios::sync_with_stdio(false);
 92     cin.tie(0);
 93 
 94     int T;
 95     cin >> T;
 96 
 97     int p = 0;
 98     int q = 0;
 99     while(T--){
100         cin >> V >> E >> S >> K >> C;
101         
102         rep(i,1,V) rep(j,1,V){
103             if(i == j) mp[i][j] = 0;
104             else mp[i][j] = inf;
105         }
106 
107         rep(i,1,K){
108             cin >> team[i];
109         }
110 
111         int u,v,w;
112 
113         rep(i,1,E){
114             cin >> u >> v >> w;
115             fired[v] = true;
116             if(mp[u][v] > w){
117                 mp[u][v] = mp[v][u] = w;
118             }
119         }
120 
121         p = dijkstra(S);//英雄
122     
123         q = dijkstra(0);//团队
124 
125         if(p <= q*C) cout << p << endl;
126         else cout << q << endl;
127     }
128 
129     getchar();getchar();
130 
131     return 0;
132 }

 

Guess you like

Origin www.cnblogs.com/SSummerZzz/p/11512251.html