Base Station
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others)Total Submission(s): 394 Accepted Submission(s): 143
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.
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.
5 5 1 2 3 4 5 1 2 3 2 3 4 1 3 3 1 4 2 4 5 3
4
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 55005
#define M 320005
#define inf 999999999
using namespace std;
int n,m,s,t,num,adj[N],dis[N],q[N];
struct edge
{
int v,w,pre;
}e[M];
void insert(int u,int v,int w)
{
e[num]=(edge){v,w,adj[u]};
adj[u]=num++;
e[num]=(edge){u,0,adj[v]};
adj[v]=num++;
}
int bfs()
{
int i,x,v,tail=0,head=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head<tail)
{
x=q[head++];
for(i=adj[x];i!=-1;i=e[i].pre)
if(e[i].w&&dis[v=e[i].v]==0)
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
int dfs(int s,int limit)
{
if(s==t)
return limit;
int i,v,tmp,cost=0;
for(i=adj[s];i!=-1;i=e[i].pre)
if(e[i].w&&dis[s]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp>0)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
if(limit==cost)
break;
}
else dis[v]=-1;
}
return cost;
}
int Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(s,INT_MAX);
return ans;
}
int main ()
{
while(~scanf("%d%d",&n,&m))
{
int i,u,v,w,sum=0;
memset(adj,-1,sizeof(adj));
num=0;
s=0;
t=n+m+1;
for(i=1;i<=n;i++)
{
scanf("%d",&w);
insert(i+m,t,w);
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
insert(s,i,w);
insert(i,u+m,inf);
insert(i,v+m,inf);
sum+=w;
}
printf("%d\n",sum-Dinic());
}
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5005
#define M 300005
#define inf 999999999
using namespace std;
int n,m,s,t,num,adj[N],dis[N],q[N],d[N];
struct edge
{
int v,w,pre;
edge(){}
edge(int vv,int ww,int p){v=vv;w=ww;pre=p;}
}e[M];
void insert(int u,int v,int w)
{
e[num]=edge(v,w,adj[u]);
adj[u]=num++;
e[num]=edge(u,0,adj[v]);
adj[v]=num++;
}
int bfs()
{
int i,x,v,tail=0,head=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head<tail)
{
x=q[head++];
for(i=adj[x];i!=-1;i=e[i].pre)
if(e[i].w&&dis[v=e[i].v]==0)
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
int dfs(int s,int limit)
{
if(s==t)
return limit;
int i,v,tmp,cost=0;
for(i=adj[s];i!=-1;i=e[i].pre)
if(e[i].w&&dis[s]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp>0)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
if(limit==cost)
break;
}
else dis[v]=-1;
}
return cost;
}
int Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(s,INT_MAX);
return ans;
}
int main ()
{
while(~scanf("%d%d",&n,&m))
{
int i,u,v,w[N],U=1000000;
memset(d,0,sizeof(d));
memset(adj,-1,sizeof(adj));
num=0;
s=0;
t=n+1;
for(i=1;i<=n;i++)
{
scanf("%d",w+i);
insert(s,i,U);
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,w);
d[u]+=w[0];
d[v]+=w[0];
insert(u,v,w[0]);
insert(v,u,w[0]);
}
for(i=1;i<=n;i++)
insert(i,t,U+2*w[i]-d[i]);
printf("%d\n",(U*n-Dinic())/2);
}
}
283



被折叠的 条评论
为什么被折叠?



