题目
题意:
给定n个点,要求使这n个点通电。有两种方法使其通电:1、花费c[i]的代价使i通电;2、选择一个通电的点i,连接i,j使得j通电,花费为他们的曼哈顿距离乘上k[i]+k[j]。求出最小通电的代价和方案。
1
≤
n
≤
2000
,
1
≤
x
i
≤
1
0
6
,
1
≤
y
i
≤
1
0
6
,
1
≤
c
i
≤
1
0
9
,
1
≤
k
i
≤
1
0
9
1≤n≤2000,1≤x_i≤10^6,1≤y_i≤10^6,1≤c_i≤10^9,1≤k_i≤10^9
1≤n≤2000,1≤xi≤106,1≤yi≤106,1≤ci≤109,1≤ki≤109
分析:
由于有点与点的关系,我们自然就想到边权了。分析后就可以想到最小生成树了,对于方法1我们直接建超级原点到i的边权为c[i]即可。完全图用prim算法,后面要我们输出的关系,利用cost数组,cost[i]表示扩展i需要花的最小花费,这样我们可以知道i这个节点是怎么来的了。
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll g[2005][2005],cost[2005];
int x[2005],y[2005],c[2005],k[2005],vis[2005],from[2005];
vector<int> t;
vector<int> t1,t2;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> x[i] >> y[i];
cost[i] = 1e18;
}
for (int i = 1; i <= n; i++)
{
cin >> c[i];
g[0][i] = c[i];
}
for (int i = 1; i <= n; i++)
{
cin >> k[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
g[i][j] = (ll)(abs(x[i]-x[j]) + abs(y[i]-y[j])) * (k[i] + k[j]);
}
}
//cout << "asd" << '\n';
ll ans = 0;
int begin;
cost[0] = 0;
for (int i = 0; i <= n; i++)
{
ll minx = 1e18;
for (int j = 0; j <= n; j++)
{
if( !vis[j] && minx > cost[j] )
{
minx = cost[j];
begin = j;
}
}
ans += minx;
vis[begin] = 1;
if( from[begin] != 0 )
{
t1.push_back(begin);
t2.push_back(from[begin]);
}
for (int j = 1; j <= n; j++)
{
if( vis[j] ) continue;
if( cost[j] > g[begin][j] && g[begin][j] > 0 )
{
from[j] = begin;
cost[j] = g[begin][j];
}
}
}
cout << ans << '\n';
for (int i = 1; i <= n; i++)
{
//cout << cost[i] << '\n';
if( cost[i] == c[i] ) t.push_back(i);
}
cout << t.size() << '\n';
for (int i = 0 ; i < t.size(); i++)
{
cout << t[i];
if( i == t.size() - 1 ) cout << '\n';
else cout << ' ';
}
cout << t1.size() << '\n';
for (int i = 0; i < t1.size(); i++)
{
cout << t1[i] << ' ' << t2[i] << '\n';
}
return 0;
}