好题,回路的问题一般都要转化为度数来做
若原图的基图不连通,或者存在某个点的入度或出度为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. c++ map 和 unordered_map的区别

    unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value.不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的ha ...

  2. systemtap没找到函数变量

    为啥systemtap没找到函数 hon@station6:~/codebox/stap/net$ sudo stap -L 'kernel.function("sock_recvmsg_n ...

  3. hw_breakpoint使用方法

    hw_breakpoint 使用方法 kprobe在 do_page_fault 函数中不能使用,那么如果真要在这里打点怎么办呢?看看hw_breakpoint是否可用: 事实证明,即便 hw_bre ...

  4. 【python】Python 字典(Dictionary)操作详解

    Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型.一.创建字典字典由键和对应值成对组成.字典也被称作关联数组或哈希表.基本语法如下: dict = {'} ...

  5. 多线程 调用多线程的方法 Runtime与ProcessBuilder

    一般我们使用Java运行其他类中的方法的时候,无论是静态调用还是动态调用,都是在当前的进程中执行的.也就是只有一个Java虚拟机实例在运行.有时候需要通过Java代码启动多个Java子进程,这样做会消 ...

  6. Metrics+ElasticSearch+grafana

    Metrics+ElasticSearch+grafana--性能监控解决方案 https://blog.csdn.net/Shiyaru1314/article/details/76906461 利 ...

  7. Citrix Netscaler负载均衡算法

    Citrix Netscaler负载均衡算法 http://blog.51cto.com/caojin/1926308 众所周知,作为新一代应用交付产品的Citrix Netscaler具有业内领先的 ...

  8. [NOIP2018 TG D1T3]赛道修建

    题目大意:$NOIP2018\;TG\;D1T3$ 题解:题目要求最短的赛道的长度最大,可以想达到二分答案,接着就是一个显然的树形$DP$. 发现对于一个点,它子树中若有两条链接起来比要求的答案大,一 ...

  9. [Leetcode] word break 拆分词语

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  10. D. Relatively Prime Graph

    Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E( ...