好题,回路的问题一般都要转化为度数来做
若原图的基图不连通,或者存在某个点的入度或出度为0则无解。
统计所有点的入度出度之差di
对于di>0的点,加边(s,i,di,0);
对于di<0的点,加边(i,t,-di,0);
对原图中的每条边(i,j),在网络中加边(i,j,inf,边权),
最小费用流的解加上原图所有边权和即为答案。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std;
struct way{int po,next,flow,cost;} e[];
const int inf=;
int pre[],p[],cur[],d[],fa[],cd[],rd[],q[];
bool v[];
int n,m,len,t; int getf(int x)
{
if (fa[x]!=x) fa[x]=getf(fa[x]);
return fa[x];
} bool check()
{
for (int i=; i<=n; i++)
if (!rd[i]||!cd[i]||getf(i)!=getf()) return ;
return ;
} void add(int x,int y,int f,int c)
{
e[++len].po=y;
e[len].flow=f;
e[len].cost=c;
e[len].next=p[x];
p[x]=len;
} void build(int x,int y, int f, int c)
{
add(x,y,f,c);
add(y,x,,-c);
} bool spfa()
{
int f=,r=;
for (int i=; i<=t; i++) d[i]=inf;
memset(v,false,sizeof(v));
d[]=; q[]=;
while (f<=r)
{
int x=q[f++];
v[x]=;
for (int i=p[x]; i!=-; i=e[i].next)
{
int y=e[i].po;
if (e[i].flow&&d[x]+e[i].cost<d[y])
{
d[y]=d[x]+e[i].cost;
pre[y]=x; cur[y]=i;
if (!v[y])
{
q[++r]=y;
v[y]=;
}
}
}
}
return d[n]<inf;
} int mincost()
{
int j,s=;
while (spfa())
{
int neck=inf;
for (int i=t; i; i=pre[i])
{
j=cur[i];
neck=min(neck,e[j].flow);
}
s+=d[t]*neck;
for (int i=t; i; i=pre[i])
{
j=cur[i];
e[j].flow-=neck;
e[j^].flow+=neck;
}
}
return s;
} int main()
{
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%d%d",&n,&m);
memset(p,,sizeof(p)); len=-;
memset(rd,,sizeof(rd));
memset(cd,,sizeof(cd));
for (int i=; i<=n; i++) fa[i]=i;
int ans=;
for (int i=; i<=m; i++)
{
int x,y,u,v,z;
scanf("%d%d%d",&x,&y,&z);
cd[++x]++;rd[++y]++;
build(x,y,inf,z);
u=getf(x),v=getf(y);
if (u!=v) fa[u]=v;
ans+=z;
}
if (!check())
{
puts("-1");
continue;
}
t=n+;
for (int i=; i<=n; i++)
if (rd[i]>cd[i]) build(,i,rd[i]-cd[i],);
else build(i,t,cd[i]-rd[i],);
ans+=mincost();
printf("%d\n",ans);
}
}

hit2739的更多相关文章

  1. HIT2739 The Chinese Postman Problem(最小费用最大流)

    题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...

随机推荐

  1. [STL] 遍历删除两个vector中交集

    #include <vector> #include <string> #include <algorithm> using namespace std; int ...

  2. WebService使用介绍(二)

    Soap soap是什么 SOAP 是一种网络通信协议 SOAP即Simple Object Access Protocol简易对象访问协议 SOAP 用于跨平台应用程序之间的通信 SOAP 被设计用 ...

  3. hdu 1286 找新朋友 (欧拉函数)

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. [CF1077C]Good Array

    题目大意:一个序列是好的当且仅当有一个数是其它所有数的和,问一个序列可以删掉哪个数变成好的序列.输出所有方案. 题解:发现等于其他数的和的那个数一定是其中最大的,只要排序一下(其实只要找到最大的两个数 ...

  5. ng 构建

    1.ng 构建和部署 构建:编译和合并ng build 部署:复制dist里面的文件到服务器 2.多环境的支持 配置环境package.json "scripts": { &quo ...

  6. 用@Component注解代替@Configuration注解,定义bean

    package com.timo.entity; import org.springframework.beans.factory.annotation.Value; import org.sprin ...

  7. IDEA 用maven创建web项目编译时不能发布resources中的文件

    1.在pom.xml加入 <build> <resources> <resource> <directory>${basedir}/src/main/j ...

  8. Ubuntu 12.04更新源(转)

    1.首先备份Ubuntu12.04源列表 sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup (备份下当前的源列表) 2.修改更新源 ...

  9. 记另类Request method 'GET' not supported

    一般遇到Request method 'GET' not supported这种问题,大家都会找相应controller下的具体方法,把get改为post之类.但是我这次是在访问静态资源,static ...

  10. 区间(bzoj 4653)

    Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x ...