hdu 3435 A new Graph Game
http://acm.hdu.edu.cn/showproblem.php?pid=3435
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
#define maxn 54444
using namespace std; queue<int>q;
struct node
{
int u,v,next,val,f;
} p[maxn];
int head[maxn];
bool vis[maxn];
int dis[maxn];
int pre[maxn];
int n,m,s,t,e; void add(int u,int v,int f,int c)
{
p[e].u=u;
p[e].v=v;
p[e].f=f;
p[e].val=c;
p[e].next=head[u];
head[u]=e++;
p[e].u=v;
p[e].v=u;
p[e].f=;
p[e].val=-c;
p[e].next=head[v];
head[v]=e++; } bool spfa()
{
int j,k,i;
while(!q.empty()) q.pop();
memset(vis,false,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
memset(pre,-,sizeof(pre));
vis[s]=true;
dis[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(j=head[u]; j!=-; j=p[j].next)
{
int v=p[j].v;
if(p[j].f&&dis[u]+p[j].val<dis[v])
{
dis[v]=dis[u]+p[j].val;
pre[v]=j;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
return dis[t]!=inf;
} int mincost()
{
int ret=,u;
while(spfa())
{
u=pre[t];
ret+=dis[t];
while(u!=-)
{
p[u].f--;
p[u^].f++;
u=pre[p[u].u];
}
}
return ret;
}
int main()
{
int case1;
scanf("%d",&case1);
for(int k=; k<=case1; k++)
{
e=;
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
s=;
t=*n+;
for(int i=; i<=m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b+n,,c);
add(b,a+n,,c);
}
for(int i=; i<=n; i++)
{
add(s,i,,);
add(i+n,t,,);
}
int j;
int ans=mincost();
for(j=head[s]; j!=-; j=p[j].next)
if(p[j].f!=) break;
if(j==-) printf("Case %d: %d\n",k,ans);
else printf("Case %d: NO\n",k);
}
return ;
}
hdu 3435 A new Graph Game的更多相关文章
- HDU 3435 A new Graph Game(最小费用流:有向环权值最小覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3435 题意:有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小. 思路: 费用 ...
- 【刷题】HDU 3435 A new Graph Game
Problem Description An undirected graph is a graph in which the nodes are connected by undirected ar ...
- HDU 3435 A new Graph Game(最小费用最大流)&HDU 3488
A new Graph Game Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【HDU 3435】 A new Graph Game (KM|费用流)
A new Graph Game Problem Description An undirected graph is a graph in which the nodes are connected ...
- HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)
6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...
- HDU 3435 KM A new Graph Game
和HDU 3488一样的,只不过要判断一下是否有解. #include <iostream> #include <cstdio> #include <cstring> ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 5631 Rikka with Graph 暴力 并查集
Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...
随机推荐
- js跨域问题新方案
只要创建一个空图片. js代码: var data = "http://localhost:8080/test?id="+id+"&content="+ ...
- motan源码分析五:cluster相关
上一章我们分析了客户端调用服务端相关的源码,但是到了cluster里面的部分我们就没有分析了,本章将深入分析cluster和它的相关支持类. 1.clustersupport的创建过程,上一章的Ref ...
- lesson9:分布式定时任务
在实际的开发过程中,我们一定会遇到服务自有的定时任务,又因为现在的服务都是分布式的,但是对于定时任务,很多的业务场景下,还是只希望单台服务来执行,网上有很多分布式定时任务的框架,各位如感兴趣,可以自行 ...
- 浅谈Manacher算法与扩展KMP之间的联系
首先,在谈到Manacher算法之前,我们先来看一个小问题:给定一个字符串S,求该字符串的最长回文子串的长度.对于该问题的求解.网上解法颇多.时间复杂度也不尽同样,这里列述几种常见的解法. 解法一 ...
- C primer plus 读书笔记第六章和第七章
这两章的标题是C控制语句:循环以及C控制语句:分支和跳转.之所以一起讲,是因为这两章内容都是讲控制语句. 第六章的第一段示例代码 /* summing.c --对用户输入的整数求和 */ #inclu ...
- [Javascript] JavaScript Array Methods in Depth - push
Array push is used to add elements to the end of an Array. In this lesson we'll see how the push met ...
- [转] Web性能压力测试工具之ApacheBench(ab)详解
PS:网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题.Apache中有个自带的,名为ab的程序,可以对Apache或其它类型的服务器进行网 ...
- C#获取窗口,模拟按键操作
C#获取窗口,模拟按键操作,实现计算器模拟操作.首先引用. using System.Runtime.InteropServices; 使用DllImport引入两个函数: // Get a hand ...
- 《AngularJS》--指令的相互调用
转载自http://blog.csdn.net/zhoukun1008/article/details/51296692 人们喜欢AngularJS,因为他很有特色,其中他的指令和双向数据绑定很吸引着 ...
- 手机端input,select屏蔽浏览器默认事件
文本框input:当文本框focus时会弹出软键盘,有时我们需要click事件而又不想触发focus事件(不要弹出软键盘) 给input添加 disabled="disabled" ...