hdu 3435(KM算法最优匹配)

时间:2022-09-18 23:58:27

A new Graph Game

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2360    Accepted Submission(s): 951

Problem Description
An
undirected graph is a graph in which the nodes are connected by
undirected arcs. An undirected arc is an edge that has no arrow. Both
ends of an undirected arc are equivalent--there is no head or tail.
Therefore, we represent an edge in an undirected graph as a set rather
than an ordered pair.
Now given an undirected graph, you could delete
any number of edges as you wish. Then you will get one or more
connected sub graph from the original one (Any of them should have more
than one vertex).
You goal is to make all the connected sub graphs
exist the Hamiltonian circuit after the delete operation. What’s more,
you want to know the minimum sum of all the weight of the edges on the
“Hamiltonian circuit” of all the connected sub graphs (Only one
“Hamiltonian circuit” will be calculated in one connected sub graph!
That is to say if there exist more than one “Hamiltonian circuit” in one
connected sub graph, you could only choose the one in which the sum of
weight of these edges is minimum).
  For example, we may get two possible sums:
hdu 3435(KM算法最优匹配)
(1)  7 + 10 + 5 = 22
(2)  7 + 10 + 2 = 19
(There are two “Hamiltonian circuit” in this graph!)
 
Input
In the first line there is an integer T, indicates the number of test cases. (T <= 20)
In
each case, the first line contains two integers n and m, indicates the
number of vertices and the number of edges. (1 <= n <=1000, 0
<= m <= 10000)
Then m lines, each line contains three integers
a,b,c ,indicates that there is one edge between a and b, and the weight
of it is c . (1 <= a,b <= n, a is not equal to b in any way, 1
<= c <= 10000)
 
Output
Output
“Case %d: “first where d is the case number counted from one. Then
output “NO” if there is no way to get some connected sub graphs that any
of them exists the Hamiltonian circuit after the delete operation.
Otherwise, output the minimum sum of weight you may get if you delete
the edges in the optimal strategy.
 
Sample Input
3

3 4
1 2 5
2 1 2
2 3 10
3 1 7

3 2
1 2 3
1 2 4

2 2
1 2 3
1 2 4

 
Sample Output
Case 1: 19
Case 2: NO
Case 3: 6
 
题意:将一个无向图删边得到一些子图,并使每个子图中存在哈密顿回路,并使所有哈密顿回路上边的权值最小。如果有,输出这个最小的子图,如果没有,输出NO。
题解:每个点的话就是出度和入度都为1了,每个点必须且仅走一次,这样的话就是二分图完美匹配了。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
int graph[N][N];
int lx[N], ly[N];
bool visitx[N], visity[N];
int slack[N];
int match[N];
int n,m;
bool Hungary(int u)
{
int temp;
visitx[u] = true;
for(int i = ; i <= n; ++i)
{
if(visity[i])
continue;
else
{
temp = lx[u] + ly[i] - graph[u][i];
if(temp == ) //相等子图
{
visity[i] = true;
if(match[i] == - || Hungary(match[i]))
{
match[i] = u;
return true;
}
}
else //松弛操作
slack[i] = min(slack[i], temp);
}
}
return false;
}
void KM()
{
int temp;
memset(match,-,sizeof(match));
memset(ly,,sizeof(ly));
for(int i = ;i <= n;i++) //定标初始化
lx[i] = -INF;
for(int i =;i<=n;i++)
for(int j=;j<= n;j++)
lx[i] = max(lx[i], graph[i][j]);
for(int i = ; i <= n;i++)
{
for(int j = ; j <= n;j++)
slack[j] = INF;
while()
{
memset(visitx,false,sizeof(visitx));
memset(visity,false,sizeof(visity));
if(Hungary(i))
break;
else
{
temp = INF;
for(int j = ; j <= n; ++j)
if(!visity[j]) temp = min(temp, slack[j]);
for(int j = ; j <= n; ++j)
{
if(visitx[j]) lx[j] -= temp;
if(visity[j]) ly[j] += temp;
else slack[j] -= temp;
}
}
}
}
}
int main()
{
int tcase;
int t= ;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
graph[i][j] = -INF;
}
}
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u==v) continue;
graph[u][v] = graph[v][u] = max(graph[u][v],-w);
}
KM();
int ans = ;
bool flag = false;
for(int i=;i<=n;i++){
if(match[i]==-||graph[match[i]][i]==-INF){
flag = true;
break;
}
ans+=graph[match[i]][i];
}
printf("Case %d: ",t++);
if(flag)printf("NO\n");
else printf("%d\n",-ans);
}
return ;
}

hdu 3435(KM算法最优匹配)的更多相关文章

  1. hdu 2448&lpar;KM算法&plus;SPFA&rpar;

    Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  2. HDU 2255 KM算法 二分图最大权值匹配

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. hdu 3488&lpar;KM算法&vert;&vert;最小费用最大流&rpar;

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  4. hdu 4862 KM算法 最小K路径覆盖的模型

    http://acm.hdu.edu.cn/showproblem.php?pid=4862 选t<=k次,t条路要经过全部的点一次而且只一次. 建图是问题: 我自己最初就把n*m 个点分别放入 ...

  5. hdu 3395&lpar;KM算法&vert;&vert;最小费用最大流&lpar;第二种超级巧妙&rpar;&rpar;

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. HDU 1533 KM算法(权值最小的最佳匹配)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. HDU 3435 KM A new Graph Game

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

  8. hdu 1853 KM算法

    #include<stdio.h> #include<math.h> #include<string.h> #define N 200 #define inf 99 ...

  9. km算法&lpar;二分图最大权匹配&rpar;学习

    啦啦啦! KM算法是通过给每个顶点一个标号(叫做顶标)来把求最大权匹配的问题转 化为求完备匹配的问题的.设顶点Xi的顶标为A[i],顶点Yi的顶标为B[i],顶点Xi与Yj之间的边权为w[i,j].在 ...

随机推荐

  1. 计算机网络学习笔记--数据链据层之MAC子层&lpar;整理&rpar;

    概述: 为什么需要介质访问控制子层(MAC)? 介质访问控制子层(MAC)是局域网体系结构中划分的子层,多路访问链路采用共享介质连接所有站点.发送站点通过广播方式发送数据并占用整个带宽,如果有多个站点 ...

  2. 【转】深入分析 iBATIS 框架之系统架构与映射原理

    深入分析 iBATIS 框架之系统架构与映射原理 iBATIS 通过 SQL Map 将 Java 对象映射成 SQL 语句和将结果集再转化成 Java 对象,与其他 ORM 框架相比,既解决了 Ja ...

  3. &lbrack; &rsqb; 字符组&lpar;Character Classes&rpar; &lpar;转&rpar;

    []能够匹配所包含的一系列字符中的任意一个.需要注意的是,[]虽然能匹配其中的任意一个字符,但匹配的结果只能是一个字符,不是多个. 例如[abc]表示字符“a”或“b”或“c”. []支持用连字符“- ...

  4. 使用PHP处理POST上传时&dollar;&lowbar;FILES数组为何为空

    在做一个简单的表单上传测试时,服务端的php脚本中,$_FILES数组为空;这样就不能获取从浏览器上传的信息.什么原因呢? 通过Google,找到下面这个web: php上传文件$_FILES数组为空 ...

  5. 通过 PowerShell 支持 Azure Traffic Manager 外部端点和权重轮询机制

    Jonathan TulianiAzure网络 - DNS和 Traffic Manager高级项目经理 在北美 TechEd 大会上,我们宣布了 Azure Traffic Manager将支持 ...

  6. 创建并发布npm包

    1.npm官网创建npm账户 npm网站地址:https://www.npmjs.com/ npm网站注册地址:https://www.npmjs.com/signup 2.命令行工具登录npm np ...

  7. 白话kubernetes的十万个为什么&lpar;持续更新中&period;&period;&period;&rpar; - kubernetes

    Kubernetes简称? 答:k8s或kube. Kubernetes是什么? 答:由Google开发的一个强大的平台,可以在集群环境中管理容器化应用程序.本质上是一种特殊的数据库,里面存储的是能够 ...

  8. ios下表单post使用gzip模式

    使用afnetworking,服务器参考的这里 ios端,使用自己的序列化类 manager.requestSerializer = [MyHttpRequestSerializer new];[ma ...

  9. python3与python2的区别&lpar;目前遇到的)

    1.进击的print,变成一个函数,print() 2.urllib大一统,呵呵 3.python3默认绝对路径导入

  10. 小白第一次使用Git随笔

    想研究Git很久了,一直没有找到很好的博客或论坛,近几天工作项目任务没有那么重,就想着找几篇文章把这玩意儿给解决掉,本博客是记录读廖雪峰老师所写的<Git教程>的随笔,以便巩固学习,若想学 ...