您的位置:首页 > 其它

HDU 3879 Base Station

2012-09-17 22:55 330 查看

Base Station

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others)

Total Submission(s): 1176 Accepted Submission(s): 506

[align=left]Problem Description[/align]
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 Pi (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 Ai, Bi and Ci, which means if place Ai
and Bi can communicate with each other, the company will get Ci 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.

[align=left]Input[/align]
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, Ai, Bi and Ci, describes the ith requirement.

[align=left]Output[/align]
One integer each case, the maximum profit of the company.

[align=left]Sample Input[/align]

5 5
1 2 3 4 5
1 2 3
2 3 4
1 3 3
1 4 2
4 5 3


[align=left]Sample Output[/align]

4


[align=left]Author[/align]
liulibo

[align=left]Source[/align]
2011 Multi-University Training Contest 5 - Host by BNU

[align=left]Recommend[/align]
lcy

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>

#define INT_INF 0x3fffffff
#define LL_INF 0x3fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 60000
#define E 2000000

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;

struct Edge
{
int en,cap,flow,next;
} edge[E];
int head
, tot , now
;
int source,sink,tot_num;
int pre
, dis
, gap
;

void add_edge(int st,int en,int cap)
{
edge[tot].en=en;
edge[tot].cap=cap;
edge[tot].flow=0;
edge[tot].next=head[st];
head[st]=tot++;

edge[tot].en=st;
edge[tot].cap=0;
edge[tot].flow=0;
edge[tot].next=head[en];
head[en]=tot++;
}

void augment(int flow)
{
for(int i=source;i!=sink;i=edge[now[i]].en)
{
edge[now[i]].flow+=flow;
edge[now[i]^1].flow-=flow;
}
}

int sap()
{
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
for(int i=0;i<tot_num;i++)
now[i]=head[i];
gap[0]=tot_num;
int point=source,flow=0,min_flow=INT_INF;
while(dis[source]<tot_num)
{
bool fg=false;
for(int i=now[point];i!=-1;i=edge[i].next)
if(edge[i].cap-edge[i].flow>0 && dis[point]==dis[edge[i].en]+1)
{
min_flow=min(min_flow,edge[i].cap-edge[i].flow);
now[point]=i;
pre[edge[i].en]=point;
point=edge[i].en;
if(point==sink)
{
flow+=min_flow;
augment(min_flow);
point=source;
min_flow=INT_INF;
}
fg=true;
break;
}
if(fg) continue;
if(--gap[dis[point]]==0) break;
int Min=tot_num;
for(int i=head[point];i!=-1;i=edge[i].next)
if(edge[i].cap-edge[i].flow>0 && Min>dis[edge[i].en])
{
Min=dis[edge[i].en];
now[point]=i;
}
gap[dis[point]=Min+1]++;
if(point!=source) point=pre[point];
}
return flow;
}

int build(int n,int m)
{
int ans=0;
memset(head,-1,sizeof(head));
tot=0;
source=0; sink=n+m+1; tot_num=n+m+2;
for(int i=1,a; i<=n; i++)
{
scanf("%d",&a);
add_edge(i,sink,a);
}
for(int i=1,a,b,c;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(i+n,a,INT_INF);
add_edge(i+n,b,INT_INF);
add_edge(source,i+n,c);
ans+=c;
}
return ans;
}

int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int ans=build(n,m);
ans-=sap();
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: