Base Station
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others)Total Submission(s): 2827 Accepted Submission(s): 1219
Problem Description
A famous mobile communication company is planning to build a new set of base stations. According to the previous investigation, n places are chosen as the possible new locations to build those new stations. However, the condition of each position varies much, so the costs to built a station at different places are different. The cost to build a new station at the ith place is P
i (1<=i<=n).
When complete building, two places which both have stations can communicate with each other.
Besides, according to the marketing department, the company has received m requirements. The ith requirement is represented by three integers A i, B i and C i, which means if place A i and B i can communicate with each other, the company will get C i profit.
Now, the company wants to maximize the profits, so maybe just part of the possible locations will be chosen to build new stations. The boss wants to know the maximum profits.
When complete building, two places which both have stations can communicate with each other.
Besides, according to the marketing department, the company has received m requirements. The ith requirement is represented by three integers A i, B i and C i, which means if place A i and B i can communicate with each other, the company will get C i profit.
Now, the company wants to maximize the profits, so maybe just part of the possible locations will be chosen to build new stations. The boss wants to know the maximum profits.
Input
Multiple test cases (no more than 20), for each test case:
The first line has two integers n (0<n<=5000) and m (0<m<=50000).
The second line has n integers, P1 through Pn, describes the cost of each location.
Next m line, each line contains three integers, A i, B i and C i, describes the ith requirement.
The first line has two integers n (0<n<=5000) and m (0<m<=50000).
The second line has n integers, P1 through Pn, describes the cost of each location.
Next m line, each line contains three integers, A i, B i and C i, describes the ith requirement.
Output
One integer each case, the maximum profit of the company.
Sample Input
5 5 1 2 3 4 5 1 2 3 2 3 4 1 3 3 1 4 2 4 5 3
Sample Output
4
题解:
将所有点与en连一条边,权值为其花费,对于每一个收益的点对(x,y),
建立一个点k,st与k连一条权值为其收益的边,然后k->x,k->y连一条
权值为无穷大的边。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=60005;
const int st=0;
const int en=60004;
const int inf=1e9;
struct node
{
int to,cap,rev;
};
vector<node>G[maxn];
int level[maxn],iter[maxn];
void add(int from,int to,int cap)
{
G[from].push_back((node){to,cap,G[to].size()});
G[to].push_back((node){from,0,G[from].size()-1});
}
void bfs(int s)
{
queue<int>P;
memset(level,-1,sizeof(level));
level[s]=0;P.push(s);
while(!P.empty())
{
int v=P.front();P.pop();
for(int i=0;i<G[v].size();i++)
{
node e=G[v][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[v]+1;
P.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<G[v].size();i++)
{
node &e=G[v][i];
if(e.cap>0&&level[e.to]>level[v])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0;
while(1)
{
bfs(s);
if(level[t]<0)return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,inf))>0)flow+=f;
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
ll sum=0;
for(int i=0;i<maxn;i++)G[i].clear();
for(int i=1;i<=n;i++)
{
int x;scanf("%d",&x);
add(i,en,x);
}
for(int i=1;i<=m;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
sum+=z;
add(st,n+i,z);
add(n+i,x,z);
add(n+i,y,z);
}
printf("%lld\n",sum-max_flow(st,en));
}
return 0;
}