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 ...
随机推荐
- poj1190 生日蛋糕 dfs
题意:生日蛋糕有m层,总体积是V.从下向上,每一层的半径r和高度h都是递减的. 给m.v,求最小的表面积s.(不算底面接地的面积) 题目链接:poj1190 剪枝都还没加..样例输出都是错的...还没 ...
- Matlab绘制三维图形以及提示框
1.首先,在编辑区输入如下代码 >> [x,y] = meshgrid([-100,0.1,100]); >> z = sqrt(x.^2 + y.^2); >> ...
- oracle用户解锁和改密
alter user hs_user account unlock; alter user hs_asset account unlock; alter user hs_his account unl ...
- 总结XX网app中webapp常见的前端错误。
在2016年12月至2017年1月,这一个月的时间内,我参与了易政网app中webapp前端项目的工作,下面将我在此次项目中犯的错误总结起来,以防下次再犯.也终于知道之前看的文章中的一段话所代表的意义 ...
- Android打包常见错误之Export aborted because fatal lint errors were found
打包时报如下错误: <ignore_js_op> Export aborted because fatal lint errors were found. These are listed ...
- 针对Yii框架的nginx配置
我曾经针对yii制作了 个nginx配置,其中包括了以下几项内容: rewrite规则(try_file),需要nginx0.8.6版本以上支持. 针对于icon, robots.txt文件的日志优化 ...
- 深入理解javascript之this
javascript中的this含义很丰富,它能够是全局对象,当前对象或者是随意对象,这都取决于函数的调用方式.函数有下面几种调用方式:作为对象方法调用.作为函数调用.作为构造函数调用.apply或c ...
- [Javascript] Array methods in depth - filter
Array filter creates a new array with all elements that pass the test implemented by the provided fu ...
- 如何解决Windows 7的多重网络问题
如何解决Windows 7的多重网络问题 出现这种现象一般是明天网络还好好的,今天一开机就上不了网了,可是局域网是通的.打开网络和共享中心你会发现PC和Internet之间是一个多重网络.造成这种 ...
- Zend Framework
参考:http://www.php100.com/manual/ZendFramework/index.html 1.1. 概述 Zend Framework (ZF) 是一个开放源代码的 PHP5 ...