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的更多相关文章

  1. HDU 3435 A new Graph Game(最小费用流:有向环权值最小覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3435 题意:有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小. 思路: 费用 ...

  2. 【刷题】HDU 3435 A new Graph Game

    Problem Description An undirected graph is a graph in which the nodes are connected by undirected ar ...

  3. HDU 3435 A new Graph Game(最小费用最大流)&amp;HDU 3488

    A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【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 ...

  5. 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 - ...

  6. HDU 3435 KM A new Graph Game

    和HDU 3488一样的,只不过要判断一下是否有解. #include <iostream> #include <cstdio> #include <cstring> ...

  7. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  8. 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 ...

  9. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

随机推荐

  1. 尚学堂 JAVA Day3 概念总结

    java中的运算符 1.算术运算符 + - * / % Arithmetic operators + 运算符有三种身份 Additive Operator 1)加法:如 a + b; 2)连接:如 “ ...

  2. 封装实现UIButton左文字右图片

    #import "TitleButton.h" @implementation TitleButton - (instancetype)initWithFrame:(CGRect) ...

  3. Java Web开发常见问题

    一.Tomcat服务器常见启动问题:(1).Java_home环境变量,由于tomcat服务器的bin目录中的一些jar文件必须使用到java类库,所以必须先配置Java_home环境变量.(2).端 ...

  4. word2vec浅析

    本文是參考神经网络语言模型.word2vec相关论文和网上博客等资料整理的学习笔记.仅记录 自己的学习历程,欢迎拍砖. word2vec是2013年google提出的一种神经网络的语言模型,通过神经网 ...

  5. Java基础知识强化95:Calendar类之Calendar类的add()和set()方法

    1. Calendar的add()和set()方法: public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作 public ...

  6. 【iOS开发】emoji表情的输入思路

    1.自定义一个表情包类继承NSTextAttachment #import <UIKit/UIKit.h> /** 表情包的自定义类*/ @interface EmojiTextAttac ...

  7. 排序算法之快速排序 JAVA快速排序算法

    public static void quickSort(int[] arr, int low , int height){ int l=low, h = height; if(low < he ...

  8. mysql的limit性能,数据库索引问题,dblog问题

    mysql的limit性能,数据库索引问题,dblog问题,redis学习 继续学习. dblog实际上是把日志记录在另一个数据库里面. 问题1: 一张表定义了5个索引,但是sql语句中用到了3个有索 ...

  9. Jason 分享吴霁虹教授的产品模型

    产品的出现都是为了解决市场上存在的某一个”疼点“或一系列的”疼点“而出现. 疼点:是一个亟需待解决的问题,对应有相应的市场,会寻找相应的解决方案.比如:用户的小孩——>因为缺钱,所以担心小孩无法 ...

  10. WPF画N角芒星,正N角星

    计算顶部三角形坐标方法: /// <summary> /// 获取顶三角形坐标 /// </summary> /// <param name="r"& ...