zoukankan      html  css  js  c++  java
  • HDU 1853 Cyclic Tour

    Cyclic Tour

    Time Limit: 1000ms
    Memory Limit: 65535KB
    This problem will be judged on HDU. Original ID: 1853
    64-bit integer IO format: %I64d      Java class name: Main
     
    There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
     

    Input

    There are several test cases in the input. You should process to the end of file (EOF).
    The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
     

    Output

    Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1. 
     

    Sample Input

    6 9
    1 2 5
    2 3 5
    3 1 10
    3 4 12
    4 1 8
    4 6 11
    5 4 7
    5 6 9
    6 5 4
    6 5
    1 2 1
    2 3 1
    3 4 1
    4 5 1
    5 6 1

    Sample Output

    42
    -1
    Hint
    In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.

    Source

     
    解题:KM最小权匹配或者费用流
     
    KM算法
     1 #include <bits/stdc++.h>
     2 #define type int
     3 using namespace std;
     4 const int INF = 0x3f3f3f3f;
     5 const int maxn = 310;
     6 type Lx[maxn],Ly[maxn],W[maxn][maxn],slack[maxn];
     7 bool S[maxn],T[maxn];
     8 int n,Link[maxn];
     9 bool match(int u) {
    10     S[u] = true;
    11     for(int v = 1; v <= n; ++v) {
    12         if(T[v]) continue;
    13         type d = Lx[u] + Ly[v] - W[u][v];
    14         if(!d) { //浮点数时需要用精度判0
    15             T[v] = true;
    16             if(Link[v] == -1 || match(Link[v])) {
    17                 Link[v] = u;
    18                 return true;
    19             }
    20         } else if(slack[v] > d) slack[v] = d;
    21     }
    22     return false;
    23 }
    24 void update() {
    25     type d = INF;
    26     for(int i = 1; i <= n; ++i)
    27         if(!T[i] && slack[i] < d)
    28             d = slack[i];
    29     for(int i = 1; i <= n; ++i) {
    30         if(S[i]) Lx[i] -= d;
    31         if(T[i]) Ly[i] += d;
    32         else slack[i] -= d;
    33     }
    34 }
    35 void KuhnMunkras() {
    36     for(int u = 1; u <= n; ++u) {
    37         Lx[u] = -INF;
    38         Ly[u] = 0;
    39         Link[u] = -1;
    40         for(int v = 1; v <= n; ++v)
    41             Lx[u] = max(Lx[u],W[u][v]);
    42     }
    43     for(int u = 1; u <= n; ++u) {
    44         for(int v = 1; v <= n; ++v) 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     type ret = 0;
    53     for(int i = 1; i <= n; ++i)
    54         if(Link[i] == -1 || W[Link[i]][i] == -INF) {
    55             puts("-1");
    56             return;
    57         } else {
    58             ret += W[Link[i]][i];
    59         }
    60     printf("%d
    ",-ret);
    61 }
    62 int m;
    63 int main() {
    64     int u,v,w;
    65     while(~scanf("%d%d",&n,&m)) {
    66         for(int i = 1; i <= n; ++i)
    67             for(int j = 1; j <= n; ++j)
    68                 W[i][j] = -INF;
    69         for(int i = 0; i < m; ++i) {
    70             scanf("%d%d%d",&u,&v,&w);
    71             W[u][v] = max(W[u][v],-w);
    72         }
    73         KuhnMunkras();
    74     }
    75     return 0;
    76 }
    View Code
     费用流(最小费用最大流)
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 10010;
     4 const int INF = 0x3f3f3f3f;
     5 struct arc {
     6     int to,flow,cost,next;
     7     arc(int x = 0,int y = 0,int z = 0,int nxt = -1) {
     8         to = x;
     9         flow = y;
    10         cost = z;
    11         next = nxt;
    12     }
    13 } e[1000010];
    14 int head[maxn],d[maxn],p[maxn],tot,S,T;
    15 int n,m;
    16 void add(int u,int v,int flow,int cost) {
    17     e[tot] = arc(v,flow,cost,head[u]);
    18     head[u] = tot++;
    19     e[tot] = arc(u,0,-cost,head[v]);
    20     head[v] = tot++;
    21 }
    22 bool inq[maxn];
    23 bool spfa() {
    24     memset(p,-1,sizeof p);
    25     memset(d,0x3f,sizeof d);
    26     queue<int>q;
    27     memset(inq,false,sizeof inq);
    28     d[S] = 0;
    29     q.push(S);
    30     while(!q.empty()) {
    31         int u = q.front();
    32         q.pop();
    33         inq[u] = false;
    34         for(int i = head[u]; ~i; i = e[i].next) {
    35             if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {
    36                 d[e[i].to] = d[u] + e[i].cost;
    37                 p[e[i].to] = i;
    38                 if(!inq[e[i].to]) {
    39                     inq[e[i].to] = true;
    40                     q.push(e[i].to);
    41                 }
    42             }
    43         }
    44     }
    45     return p[T] > -1;
    46 }
    47 void solve() {
    48     int ret = 0,flow = 0;
    49     while(spfa()) {
    50         int minF = INF;
    51         for(int i = p[T]; ~i; i = p[e[i^1].to])
    52             minF = min(minF,e[i].flow);
    53         for(int i = p[T]; ~i; i = p[e[i^1].to]) {
    54             e[i].flow -= minF;
    55             e[i^1].flow += minF;
    56         }
    57         ret += d[T]*minF;
    58         flow += minF;
    59     }
    60     if(flow != n) puts("-1");
    61     else printf("%d
    ",ret);
    62 }
    63 
    64 int main() {
    65     int u,v,w;
    66     while(~scanf("%d%d",&n,&m)) {
    67         memset(head,-1,sizeof head);
    68         S = n*2 + 2;
    69         T = S + 1;
    70         for(int i = tot = 0; i < m; ++i) {
    71             scanf("%d%d%d",&u,&v,&w);
    72             add(u,v+n,1,w);
    73         }
    74         for(int i = 1; i <= n; ++i) {
    75             add(S,i,1,0);
    76             add(i+n,T,1,0);
    77         }
    78         solve();
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    纯新手向解题WriteUp—攻防世界Web新手训练12题
    2019-2020-1学期 20192406《网络空间安全专业导论》第十二周学习总结
    2019-2020-1学期 20192406《网络空间安全专业导论》第十一周学习总结
    网络空间安全导论第4章小组讨论
    2019-2020-1学期 20192406《网络空间安全专业导论》第十周学习总结
    网络空间安全导论第3章小组讨论
    2019-2020-1学期 20192406《网络空间安全专业导论》第九周学习总结
    2019-2020-1学期 20192406《网络空间安全专业导论》第八周学习总结
    小组讨论——12-14章
    2019-2020-1学期 20192406《网络空间安全专业导论》第七周学习总结
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4678470.html
Copyright © 2011-2022 走看看