CF1245D Shichikuji and Power Grid

思路:

图论。参考了https://codeforces.com/blog/entry/71080

实现:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll INF = 0x3f3f3f3f3f3f3f3f;
 5 const int N = 2005;
 6 ll x[N], y[N], c[N], k[N];
 7 ll dis(int i, int j)
 8 {
 9     return abs(x[i] - x[j]) + abs(y[i] - y[j]);
10 }
11 ll cost(int i, int j)
12 {
13     return (k[i] + k[j]) * dis(i, j); 
14 }
15 int main()
16 {
17     int n;
18     while (cin >> n)
19     {
20         for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
21         for (int i = 0; i < n; i++) cin >> c[i];
22         for (int i = 0; i < n; i++) cin >> k[i];
23         vector<bool> vis(n, false);
24         vector<int> ch(n, -1);
25         for (int i = 0; i < n; i++) ch[i] = i;
26         ll res = 0;
27         for (int t = 0; t < n; t++)
28         {
29             ll minn = INF; int index = -1; 
30             for (int i = 0; i < n; i++)
31             {
32                 if (!vis[i] and c[i] < minn) 
33                 {
34                     minn = c[i]; index = i;
35                 }
36             }
37             vis[index] = true;
38             res += minn;
39             for (int i = 0; i < n; i++)
40             {
41                 if (vis[i]) continue;
42                 ll tmp = cost(index, i);
43                 if (tmp < c[i]) { c[i] = tmp; ch[i] = index; }
44             }
45         }
46         cout << res << endl;
47         vector<int> pow;
48         vector<pair<int, int>> r;
49         for (int i = 0; i < n; i++)
50         {
51             if (ch[i] == i) pow.push_back(i); 
52             else r.push_back({i, ch[i]}); 
53         }
54         cout << pow.size() << endl;
55         for (auto it: pow) cout << it + 1 << " ";
56         cout << endl;
57         cout << r.size() << endl;
58         for (auto it: r)
59         {
60             cout << it.first + 1 << " " << it.second + 1 << endl;
61         }
62     }
63     return 0;
64 }
原文地址:https://www.cnblogs.com/wangyiming/p/14766754.html