2055: 80人环游世界
题意:n个点带权图,选出m条路径,每个点经过val[i]次,求最小花费
建图比较简单
s拆点限制流量m
一个点拆成两个,限制流量val[i],需要用上下界
图中有边的连边,容量INF权值为花费
上下界最小费用流
那些容量为0的边不加也可以
该死我把费用流的加边打错了查了一会...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define fir first
#define sec second
typedef long long ll;
const int N=1005, M=4e5+5, INF=1e9;
inline ll read(){
char c=getchar();ll x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int n, m, s, s0, t, tot, extra[N], x;
struct edge{int v, c, f, w, ne, lower;}e[M];
int cnt=1, h[N];
inline void ins(int u, int v, int c, int w=0, int lower=0) {
e[++cnt] = (edge){v, c, 0, w, h[u], lower}; h[u]=cnt;
e[++cnt] = (edge){u, 0, 0, -w, h[v], lower}; h[v]=cnt;
}
int q[N], head, tail, d[N], inq[N];
pair<int, int> pre[N];
inline void lop(int &x) {if(x==N) x=1;}
bool spfa(int s, int t) {
memset(inq, 0, sizeof(inq));
memset(d, 127, sizeof(d));
head=tail=1;
q[tail++]=s; d[s]=0; inq[s]=1;
pre[t].fir = -1;
while(head!=tail) {
int u=q[head++]; inq[u]=0; lop(head);
for(int i=h[u];i;i=e[i].ne) {
int v=e[i].v;
if(e[i].c > e[i].f && d[v]>d[u]+e[i].w) {
d[v]=d[u]+e[i].w;
pre[v] = make_pair(u, i);
if(!inq[v]) q[tail++]=v, inq[v]=1, lop(tail);
}
}
}
return pre[t].fir != -1;
}
int ek(int s, int t) {
int flow=0, cost=0, x;
while(spfa(s, t)) {
int f=INF;
for(int i=t; i!=s; i=pre[i].fir)
f=min(f, e[pre[i].sec].c - e[pre[i].sec].f);
flow+=f; cost+=f*d[t];
//printf("f %d %d
",f,d[t]);
for(int i=t; i!=s; i=pre[i].fir)
x=pre[i].sec, e[x].f+=f, e[x^1].f-=f;
}
//printf("ek %d %d
",flow,cost);
return cost;
}
int main() {
freopen("in","r",stdin);
n=read(); m=read(); s0=0; s=n+n+1; t=n+n+2;
ins(s0, s, 0, 0, m);
extra[s0]-=m, extra[s]+=m;
for(int i=1; i<=n; i++) {
x=read();
ins(s, i, INF); ins(i+n, t, INF);
ins(i, i+n, 0, 0, x);
extra[i]-=x, extra[i+n]+=x;
}
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++) {
int val=read();
if(val!=-1) ins(i+n, j, INF, val, 0);
}
int ss=t+1, tt=t+2, sum=0; tot=tt;
for(int i=s0; i<=t; i++) {
if(extra[i]>0) ins(ss, i, extra[i]), sum+=extra[i];
if(extra[i]<0) ins(i, tt, -extra[i]);
}
ins(t, s0, INF);
int ans=ek(ss, tt);
printf("%d", ans);
}