CodeForcesGym 100641E Inspectors

Inspectors

Time Limit: 1000ms

Memory Limit: 262144KB

This problem will be judged on CodeForcesGym. Original ID: 100641E
64-bit integer IO format: %I64d      Java class name: (Any)

Yuri is a rich business owner. His company has factories in many cities across Russia, all of which need regular, monthly inspections. Now Yuri not only has a large portfolio, but a large family as well and he would like to see some of his children start to learn how the business is run. He gures the best way to do this is to have them perform the factory inspections, but he has two concerns. First, he wants each child to be assigned at least two dierent factories { that way they will be able to compare dierent management styles at the factories and get a better idea of what works and what doesn‘t work. This of course requires them to travel between the factories, which brings up Yuri‘s second problem: while
he won‘t be paying his children anything (their weekly allowance is more than adequate), he‘s worried about the total of all the travel costs, and would like it to be as small as possible. Each child is expected to inspect each of their assigned factories once a month, always ending back at the factory they started at, so to minimize cost he gures he need only assign the factories to minimize the total distance of these factory tours. The more he thinks about this, the more important saving money becomes. He‘s willing to use as few as only one of his children if that‘s the cheapest way to inspect all the factories. For example, if Yuri had four factories placed as in the gure on the left below, he could employ just
one child, who could visit each factory for a monthly distance of 40 (assume here that the unlisted distances between factories are all > 10). However, if his factories were located as shown on the right, he would need to employ two children to obtain the monthly minimal distance of 40 (note: he could
use two children to achieve the minimal distance in the rst gure as well).

Since the number of factories changes over time, Yuri would like a program to determine the minimum distance he can expect for any layout of factories.

Input

The input le begins with a line containing a single integer m indicating the number of test cases. Each
test case starts with an integer n indicating the number of factories, where 2 n 100. The next n??1
lines contain the positive integer distances between the factories: line 1 contains n??1 values specifying
the distances between factory 1 and factories 2; 3; 4; : : : ; n; line 2 contains n ?? 2 values specifying the
distances between factory 2 and factories 3; 4; : : : ; n; and so on. The maximum distance between any
two factories is 1000. All distances between any three cities will satisfy the triangle inequality.

Output
For each test case display the minimum factory tour distance. You should always assume that Yuri has
at least n=2 kids for each set of n factories.
Sample Input

2
4
10 20 10
10 20
10
4
15 20 10
10 20
15

Sample Output
Case 1: 40
Case 2: 40

解题:KM最小权匹配

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 110;
 4 const int INF = 0x3f3f3f3f;
 5 int W[maxn][maxn],Lx[maxn],Ly[maxn],slack[maxn];
 6 int n,Link[maxn];
 7 bool S[maxn],T[maxn];
 8 bool match(int u){
 9     S[u] = true;
10     for(int v = 0; v < n; ++v){
11         if(T[v]) continue;
12         int d = Lx[u] + Ly[v] - W[u][v];
13         if(!d){
14             T[v] = true;
15             if(Link[v] == -1 || match(Link[v])){
16                 Link[v] = u;
17                 return true;
18             }
19         }else if(slack[v] > d) slack[v] = d;
20     }
21     return false;
22 }
23 void update(){
24     int d = INF;
25     for(int v = 0; v < n; ++v)
26         if(!T[v] && slack[v] < d)
27             d = slack[v];
28     for(int u = 0; u < n; ++u){
29         if(S[u]) Lx[u] -= d;
30         if(T[u]) Ly[u] += d;
31         else slack[u] -= d;
32     }
33 }
34 int KM(){
35     for(int u = 0; u < n; ++u){
36         Lx[u] = -INF;
37         Ly[u] = 0;
38         Link[u] = -1;
39         for(int v = 0; v < n; ++v)
40             Lx[u] = max(Lx[u],W[u][v]);
41     }
42     for(int u = 0; u < n; ++u){
43         for(int v = 0; v < n; ++v)
44             slack[v] = INF;
45         while(true){
46             memset(S,false,sizeof S);
47             memset(T,false,sizeof T);
48             if(match(u)) break;
49             update();
50         }
51     }
52     int ret = 0;
53     for(int v = 0; v < n; ++v)
54         if(Link[v] > -1) ret += W[Link[v]][v];
55     return ret;
56 }
57 int main(){
58     int kase,cs = 1,tmp;
59     scanf("%d",&kase);
60     while(kase--){
61         for(int i = 0; i < maxn; ++i) W[i][i] = -INF;
62         scanf("%d",&n);
63         for(int i = 0; i < n-1; ++i){
64             for(int j = i + 1; j < n; ++j){
65                 scanf("%d",&tmp);
66                 W[i][j] = W[j][i] = -tmp;
67             }
68         }
69         printf("Case %d: %d\n",cs++,-KM());
70     }
71     return 0;
72 }

时间: 2025-01-06 23:55:01

CodeForcesGym 100641E Inspectors的相关文章

Unity Editor Extensions – Custom Inspectors

?? 转载请注明出处:http://blog.csdn.net/u010019717 更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/space-uid-18602.html 这是在"Unity Editor Extension"系列的第 2 次帖子. post描述了为创建自定义inspectors面板在 Unity 编辑器的基本步骤.在该系列的下一个posts,我将深入探讨更高级的主题,例如inspectors及Unity's serializa

CodeForcesGym 100212E Long Dominoes

Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. Original ID: 100212E64-bit integer IO format: %I64d      Java class name: (Any) Find the number of ways to tile an m*n rectangle with long dominoes -

CodeForcesGym 100753E Change of Scenery

Change of Scenery Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753E64-bit integer IO format: %I64d      Java class name: (Any) 解题:最短路 1 #include <bits/stdc++.h> 2 using namespace std; 3 typ

CodeForcesGym 100753F Divisions

Divisions Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753F64-bit integer IO format: %I64d      Java class name: (Any) 解题:大数质因子分解 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef

CodeForcesGym 100753B Bounty Hunter II

Bounty Hunter II Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753B64-bit integer IO format: %I64d      Java class name: (Any) 解题:最小路径覆盖 1 #include <bits/stdc++.h> 2 using namespace std; 3 co

CodeForcesGym 100502E Opening Ceremony

Opening Ceremony Time Limit: 5000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502E64-bit integer IO format: %I64d      Java class name: (Any) For the grand opening of the algorithmic games in NlogNsglow, a r

CodeForcesGym 100517B Bubble Sort

Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100517B64-bit integer IO format: %I64d      Java class name: (Any) 解题:我们发现假设当前位选择1,那么发现比1大的并且没有使用的有b个,那么当前为选1,后面就还有$2^{b-1}$种方式,所以贪心的选,只要

CodeForcesGym 100753A A Journey to Greece

A Journey to Greece Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753A64-bit integer IO format: %I64d      Java class name: (Any) 解题:状压dp 1 #include <bits/stdc++.h> 2 using namespace std; 3 t

CodeForcesGym 100502D Dice Game

Dice Game Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502D64-bit integer IO format: %I64d      Java class name: (Any) Gunnar and Emma play a lot of board games at home, so they own many dice