标签:inline code lld typedef names cpp its cout type
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2000 + 10;
int n;
ll ans, x[maxn], y[maxn], k[maxn], c[maxn];
struct Node{
int x, y;
ll z;
}p[maxn*maxn];
int tot;
void add_edge(int x, int y, ll z){
p[++tot].x = x, p[tot].y = y, p[tot].z = z;
}
int fa[maxn];
bool cmp(Node a, Node b){
return a.z < b.z;
}
int get_fa(int x)
{
if(x == fa[x]) return x;
return fa[x] = get_fa(fa[x]);
}
int ans1[maxn], cnt1;
int ax[maxn], ay[maxn], cnt2;
void kruskal()
{
for(int i = 0; i <= n; i++) fa[i] = i;
sort(p+1, p+1+tot, cmp);
int cnt = 0;
for(int i = 1; i <= tot; i++)
{
int x = p[i].x, y = p[i].y;
ll z = p[i].z;
int fx = get_fa(x), fy = get_fa(y);
if(fx != fy)
{
if(x == 0 || y == 0) ans1[++cnt1] = x + y;
else ax[++cnt2] = x, ay[cnt2] = y;
ans += z; fa[fx] = fy; cnt++;
}
if(cnt == n) break;
}
cout << ans << endl;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lld%lld", &x[i], &y[i]);
for(int i = 1; i <= n; i++) scanf("%lld", &c[i]);
for(int i = 1; i <= n; i++) scanf("%lld", &k[i]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
if(i != j)
{
ll w = (k[i] + k[j]) * (abs(x[i]-x[j]) + abs(y[i]-y[j]));
add_edge(i, j, w);
}
}
for(int i = 1; i <= n; i++)
add_edge(0, i, c[i]), add_edge(i, 0, c[i]);
kruskal();
cout << cnt1 << endl;
for(int i = 1; i <= cnt1; i++)
printf("%d ", ans1[i]); puts("");
cout << cnt2 << endl;
for(int i = 1; i <= cnt2; i++)
printf("%d %d\n", ax[i], ay[i]);
return 0;
}
CF1245D: Shichikuji and Power Grid
标签:inline code lld typedef names cpp its cout type
原文地址:https://www.cnblogs.com/zxytxdy/p/11781292.html