zoukankan      html  css  js  c++  java
  • HDU 2121 Ice_cream’s world II

    Ice_cream’s world II

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2121
    64-bit integer IO format: %I64d      Java class name: Main
     
    After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing.
     

    Input

    Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.
     

    Output

    If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank.
     

    Sample Input

    3 1
    0 1 1
    
    4 4
    0 1 10
    0 2 10
    1 3 20
    2 3 30

    Sample Output

    impossible
    
    40 0

    Source

     
    解题:最小树形图。由于我们加的虚拟根与边有关,所以可以方便的找出实际根
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int maxn = 1010;
     5 struct arc {
     6     int u,v,w;
     7 } e[50000];
     8 int in[maxn],pre[maxn],hs[maxn],vis[maxn],Rt;
     9 int DMST(int root,int n,int m,int ret = 0) {
    10     while(true) {
    11         for(int i = 0; i < n; ++i) {
    12             hs[i] = vis[i] = -1;
    13             in[i] = INF;
    14         }
    15         for(int i = 0; i < m; ++i) {
    16             if(e[i].u != e[i].v && e[i].w < in[e[i].v]) {
    17                 in[e[i].v] = e[i].w;
    18                 pre[e[i].v] = e[i].u;
    19                 if(e[i].u == root) Rt = i;
    20             }
    21         }
    22         for(int i = 0; i < n; ++i)
    23             if(root != i && in[i] == INF) return -1;
    24         int cnt = in[root] = 0;
    25         for(int i = 0; i < n; ++i) {
    26             ret += in[i];
    27             int v = i;
    28             while(vis[v] != i && hs[v] == -1 && v != root) {
    29                 vis[v] = i;
    30                 v = pre[v];
    31             }
    32             if(v != root && hs[v] == -1) {
    33                 for(int u = pre[v]; u != v; u = pre[u]) hs[u] = cnt;
    34                 hs[v] = cnt++;
    35             }
    36         }
    37         if(!cnt) break;
    38         for(int i = 0; i < n; ++i)
    39             if(hs[i] == -1) hs[i] = cnt++;
    40         for(int i = 0; i < m; ++i) {
    41             int u = e[i].u;
    42             int v = e[i].v;
    43             e[i].u = hs[u];
    44             e[i].v = hs[v];
    45             if(e[i].u != e[i].v) e[i].w -= in[v];
    46         }
    47         n = cnt;
    48         root = hs[root];
    49     }
    50     return ret;
    51 }
    52 int main() {
    53     int n,m,sum;
    54     while(~scanf("%d%d",&n,&m)) {
    55         for(int i = sum = 0; i < m; ++i) {
    56             scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    57             sum += e[i].w;
    58         }
    59         sum++;
    60         for(int i = 0; i < n; ++i) {
    61             e[i + m].u = n;
    62             e[i + m].v = i;
    63             e[i + m].w = sum;
    64         }
    65         int ret = DMST(n,n + 1,n + m);
    66         if(ret == -1 || ret > (sum<<1)) puts("impossible");
    67         else printf("%d %d
    ",ret-sum,Rt - m);
    68         puts("");
    69     }
    70     return 0;
    71 }
    View Code
  • 相关阅读:
    Delphi中三种方法获取Windows任务栏的高度
    Qt中QFtp获取带有中文的文件名称出现乱码的解决方法(比较巧妙,toLatin1压缩掉了QString自动给每个英文字符加上的那些00字节)
    Qt在Linux环境下应用程序字体模糊的解决方法(先改成使用默认字体,然后使用qtconfig配置)
    关于Qt信号与槽机制的传递方向性研究(结论其实是错误的,但是可以看看分析过程)
    RCP:如何把Preferences中的项从一个类别移动到另一个类别 2013-08-23 18:59 by Binhua Liu,
    服务端套接字类CxServerSocket的使用
    ASP.NET MVC原理
    jQuery 2.0.3 源码分析core
    CodeBlocks的下载安装、配置、简单编程
    ASP.NET MVC学习笔记-----Filter2
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4761665.html
Copyright © 2011-2022 走看看