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
  • 相关阅读:
    JavaEE学习之Spring Security3.x——模拟数据库实现用户,权限,资源的管理
    Couchbase入门——环境搭建以及HelloWorld
    VS2010如何使用Visual Studio Online在线服务管理团队资源(在线TFS)
    JSP报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    JavaEE学习之JAXB
    JavaEE学习之JPA中配置文件persistence.xml
    女朋友手速太慢,导致我无精打采。
    手机投屏之使命召唤
    智能灯改造计划
    女朋友老是埋怨我技术不行,于是我做了个辅助工具。
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4678470.html
Copyright © 2011-2022 走看看