您的位置:首页 > 其它

[BZOJ1927][Sdoi2010]星际竞速(费用流)

2017-02-04 19:10 447 查看

题目描述

传送门

题解

这道题有每一个点经过且只经过一次的限制以为是有上下界的费用流

然后写写写…对是对了,T得好惨

因为加了n2条边…

实际上这道题一个普通的费用流就能解决了

对于每一个点拆点xiyi

s->xi,1,0

s->yi,1,ai

yi->t,1,0

对于能到达的两个点ij,xi->yj,time

这样跑一遍费用流就可以了

可以发现是一定可以满流的,保证了每一个点都被访问一次,有点最小路径覆盖的意思

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
#define N 80000
#define inf 2000000000

int n,m,x,y,z,s,t,mincost;
int tot,point
,nxt
,v
,remain
,c
;
int d
,dis
,last
;
bool vis
;
queue <int> q;

void addedge(int x,int y,int cap,int z)
{
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap; c[tot]=z;
++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; c[tot]=-z;
}
int addflow(int s,int t)
{
int now=t,ans=inf;
while (now!=s)
{
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s)
{
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
bool spfa(int s,int t)
{
memset(dis,127,sizeof(dis));dis[s]=0;
memset(vis,0,sizeof(vis));vis[s]=1;
while (!q.empty()) q.pop();q.push(s);
while (!q.empty())
{
int now=q.front();q.pop();
vis[now]=0;
for (int i=point[now];i!=-1;i=nxt[i])
if (dis[v[i]]>dis[now]+c[i]&&remain[i])
{
dis[v[i]]=dis[now]+c[i];
last[v[i]]=i;
if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
}
}
if (dis[t]>inf) return 0;
int flow=addflow(s,t);
mincost+=flow*dis[t];
return 1;
}
int main()
{
tot=-1;memset(point,-1,sizeof(point));
scanf("%d%d",&n,&m);
s=n+n+1,t=s+1;
for (int i=1;i<=n;++i)
{
scanf("%d",&x);
addedge(s,n+i,1,x);
addedge(s,i,1,0);
addedge(n+i,t,1,0);
}
for (int i=1;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&z);
if (x==y) continue;
if (x>y) swap(x,y);
addedge(x,n+y,inf,z);
}
while (spfa(s,t));
printf("%d\n",mincost);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: