题目描述:
The volcanic island of Fleeland has never had a proper electric net, but finally the Biomass Alternative Power Conglomerate (BAPC) has agreed to build the island’s power plants and network.
On the island’s coast are its n cities. The BAPC has surveyed the cities and proposed m of them as possible locations for a power plant, with the ith proposal stating that the company can build a plant in city ci for cost ai.
These power plants are very modern and a single plant could power the whole island, but the volcano makes building power lines across the island a dangerous affair. For 1≤i<n, the company can build power lines between cities i and i+1 for a cost of bi, and between cities n and 1 for a cost of bn. A city will receive power if it contains a power plant or is connected to a city with a power plant via power lines.
What is the cheapest way to power all the cities on the island?
输入描述:
The input consists of:
-
One line containing two integers nnn (3≤n≤105) and mmm (1≤m≤n), the number of cities and the number of possible locations for a power plant.
-
Then follow m lines, the ith of which contains ci(1≤ci≤n) and ai (1≤ai≤109), the ith possible location for a power plant, and the cost to build it.
-
Then follows a line containing nnn integers bi(1≤bi≤109), the costs of building the power lines.
The values of c1,…,cm are unique and given in strictly increasing order.
输出描述:
Output the minimal cost of powering all cities on the island.
输入样例:
3 2
1 100
2 200
150 300 150
输出样例:
400
大概思路:
一个城市,要么自己有发电站,要么靠电线连接别的城市获取。对于后者,城市两端有电线,花费少的应该优先被建立(贪心)。
一开始,假设建立全部发电站,用连线的方式去优化。
依据贪心原则将电线按照花费v递增排序,对于某条电线,它是否应该被建立,考虑以下两种情况:
1、电线某一端的城市无电,则此线必须被建立,ans+=v;
2、电线两端都有电,看能否通过建立此电线来关闭两者中花费较高的发电站。
若干个城市通过电线相连后只需要开启一个发电站就可以了,用并查集维护。
细节见代码注释。
代码如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+50;
const ll inf=1e18;
ll a[N];//存发电站花费
int pre[N],num[N];//用于并查集
struct node{//用于电线排序
int x,y;
ll v;
}b[N];
int f(int x)//并查集查找祖先
{
if(pre[x]==x)
return x;
return pre[x]=f(pre[x]);
}
void merge(int x,int y)//两组城市通过电线合并
{
int fx=f(x);
int fy=f(y);
if(fx==fy)return;
if(num[fx]>num[fy])
{
pre[fy]=fx;
num[fx]+=num[fy];
}
else
{
pre[fx]=fy;
num[fy]+=num[fx];
}
return;
}
bool cmp(node x,node y)//电线排序
{
if(x.v!=y.v)
return x.v<y.v;
return x.x<y.x;
}
int main()
{
int n,m,p;
ll ans=0;
cin>>n>>m;
for(int i=1;i<=n;i++)//初始化
{
a[i]=inf;
pre[i]=i;
num[i]=1;
}
for(int i=0;i<m;i++)//输入发电站信息,并假设开启全部发电站
{
scanf("%d",&p);
scanf("%lld",&a[p]);
ans+=a[p];
}
for(int i=1;i<=n;i++)//输入电线信息
{
scanf("%lld",&b[i].v);
b[i].x=i;
if(i<n)b[i].y=i+1;
else b[i].y=1;
}
sort(b+1,b+n+1,cmp);//电线排序
for(int i=1;i<=n;i++)//主要过程,用电线优化,来关闭高花费的发电站
{
int x=b[i].x,y=b[i].y,v=b[i].v;
int fx=f(x),fy=f(y);
if(fx==fy)continue;
if(a[fx]==inf||a[fy]==inf)//某端无电,之间建立
{
ans+=v;
ll t=min(a[fx],a[fy]);
merge(fx,fy);
a[f(fx)]=t;
}
else if(a[fx]>a[fy])//fx端发电站的费用多
{
if(v<a[fx])//判断能否通过建立电线来优化
{
ans-=a[fx]-v;
ll t=a[fy];
merge(fx,fy);
a[f(fx)]=t;
}
}
else//fy端发电站的费用多
{
if(v<a[fy])//判断能否通过建立电线来优化
{
ans-=a[fy]-v;
ll t=a[fx];
merge(fx,fy);
a[f(fx)]=t;
}
}
}
cout<<ans<<endl;
return 0;
}