码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForcesGym 100641E Inspectors

时间:2015-08-25 21:17:12      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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 di erent factories { that way they will be able to compare di erent 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 }
View Code

 

CodeForcesGym 100641E Inspectors

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4758542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!