HDU 3435 KM A new Graph Game

和HDU 3488一样的,只不过要判断一下是否有解。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 using namespace std;
  7
  8 const int maxn = 1000 + 10;
  9 const int INF = 0x3f3f3f3f;
 10
 11 int n, m;
 12
 13 int W[maxn][maxn], lft[maxn];
 14 int slack[maxn];
 15 int Lx[maxn], Ly[maxn];
 16 bool S[maxn], T[maxn];
 17
 18 bool match(int u)
 19 {
 20     S[u] = true;
 21     for(int v = 1; v <= n; v++) if(!T[v])
 22     {
 23         int t = Lx[u] + Ly[v] - W[u][v];
 24         if(0 == t)
 25         {
 26             T[v] = true;
 27             if(!lft[v] || match(lft[v]))
 28             {
 29                 lft[v] = u;
 30                 return true;
 31             }
 32         }
 33         else slack[v] = min(slack[v], t);
 34     }
 35
 36     return false;
 37 }
 38
 39 void update()
 40 {
 41     int a = INF;
 42     for(int i = 1; i <= n; i++) if(!T[i]) a =  min(a, slack[i]);
 43     for(int i = 1; i <= n; i++)
 44     {
 45         if(S[i]) Lx[i] -= a;
 46
 47         if(T[i]) Ly[i] += a;
 48         else  slack[i] -= a;
 49     }
 50 }
 51
 52 void KM()
 53 {
 54     memset(Ly, 0, sizeof(Ly));
 55     memset(lft, 0, sizeof(lft));
 56     for(int i = 1; i <= n; i++) Lx[i] = -INF;
 57     for(int i = 1; i <= n; i++)
 58         for(int j = 1; j <= n; j++) Lx[i] = max(Lx[i], W[i][j]);
 59
 60     for(int i = 1; i <= n; i++)
 61     {
 62         memset(slack, 0x3f, sizeof(slack));
 63         for(;;)
 64         {
 65             memset(S, false, sizeof(S));
 66             memset(T, false, sizeof(T));
 67             if(match(i)) break;
 68             update();
 69         }
 70     }
 71 }
 72
 73 int main()
 74 {
 75     int T; scanf("%d", &T);
 76     for(int kase = 1; kase <= T; kase++)
 77     {
 78         scanf("%d%d", &n, &m);
 79         for(int i = 1; i <= n; i++)
 80             for(int j = 1; j <= n; j++)
 81                 W[i][j] = -INF;
 82         for(int i = 0; i < m; i++)
 83         {
 84             int u, v, d; scanf("%d%d%d", &u, &v, &d);
 85             if(W[u][v] < -d) W[u][v] = W[v][u] = -d;
 86         }
 87
 88         KM();
 89
 90         bool ok = true;
 91         int ans = 0;
 92         for(int i = 1; i <= n; i++)
 93         {
 94             if(W[lft[i]][i] == -INF) { ok = false; break; }
 95             ans += W[lft[i]][i];
 96         }
 97         printf("Case %d: ", kase);
 98         if(!ok) puts("NO");
 99         else printf("%d\n", -ans);
100     }
101
102     return 0;
103 }

代码君

时间: 2024-08-03 11:22:27

HDU 3435 KM A new Graph Game的相关文章

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

hdu 1853 Cyclic Tour &amp;&amp; hdu 3435 A new Graph Game(简单KM算法)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1478    Accepted Submission(s): 750 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

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

http://acm.hdu.edu.cn/showproblem.php?pid=4862 选t<=k次,t条路要经过所有的点一次并且仅仅一次, 建图是问题: 我自己最初就把n*m 个点分别放入X集合以及Y集合,再求最优匹配,然后连样例都过不了,而且其实当时解释不了什么情况下不能得到结果,因为k此这个条件相当于没用上... 建图方法: 1.X集合和Y集合都放入n*m+k个点,X中前n*m个点和Y中前n*m个点之间,如果格子里的值相等,权就是(收益-耗费),不等就是(-耗费),因为要的是最大收益

HDU 2454 Degree Sequence of Graph G (可简单图化的判定 havel定理)

题目链接:HDU 2454 Degree Sequence of Graph G 题意:给出N个点的度(简单图),问能能否画出个图,(其实就是给出一个串非负的序列是否有对应的图存在) 没见过这个定理 题意真的难懂. havel定理就是一个给出一串非负的序列,存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化.简单图的话就是,可简单图化. 可简单图化的判定(Havel定理):把序列排成不增序,即d1>=d2>=-->=dn,则d可简单图化当且仅当d'={d2-1,d3-1,-

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

http://acm.hdu.edu.cn/showproblem.php?pid=3435 题意:有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小. 思路: 费用流,注意判断重边,否则会超时. 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<queue> 6 using names

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

A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1849    Accepted Submission(s): 802 Problem Description An undirected graph is a graph in which the nodes are connected by undir

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) Total Submission(s): 1849    Accepted Submission(s): 802 Problem Description An undirected graph is a graph in which the nodes are connected by undir

hdu 3488(KM算法||最小费用最大流)

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2925    Accepted Submission(s): 1407 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000

HDU 3315 KM My Brute

参考题解 二分图的最优匹配.图很容易建立.再处理相似度的时候.把每个权值扩大100倍.然后再对i==j时 特殊标记.使他们的权值再++1.后面选择的时候就很容易挑出.按原匹配 匹配的个数. 100*(double)(res%100)/n.即可得到第二问. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <ve