zoukankan      html  css  js  c++  java
  • HDU 2121 Ice_cream’s world II 最小树形图 模板

    开始学习最小树形图,模板题。 

    Ice_cream’s world II

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    【Problem Description】
    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

    【分析】

    一道裸的最小树形图模板题,算法思路比较简单,但是写起来感觉很不优美...仅作为模板记录吧。

    本题不确定起点,故在读入完数据之后建立一个虚根,把所有的点都与这个虚根相连,边权为所有边边权和+1(保证虚边足够长),最后减掉即可。

    确定起点的题可以从模板中删掉这部分。

    然后就是这个模板是从0开始存储...强迫症好不爽啊...

    详细参见:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html

      1 /* ***********************************************
      2 MYID    : Chen Fan
      3 LANG    : G++
      4 PROG    : HDU 2121
      5 ************************************************ */
      6 
      7 #include <iostream>
      8 #include <cstdio>
      9 #include <cstring>
     10 #include <algorithm>
     11 
     12 using namespace std;
     13 const int N = 1010;
     14 const int M = N*N;
     15 
     16 typedef struct nod
     17 {
     18     int a,b,c;
     19 } node;
     20 
     21 node edge[M];
     22 
     23 int predge[N],id[N],visit[N];//id用来标记圈的
     24 int in[N];//入弧最小的
     25 
     26 int ansroot;
     27 
     28 int dmst(int root,int n,int m)//n表示点数,m表示边数,root表示根
     29 {
     30     int u,v;
     31     int ret=0;
     32     while(true)
     33     {
     34         for(int i=0;i<n;i++) in[i]=INT_MAX;
     35         for(int i=0;i<m;i++)
     36         {
     37             if(edge[i].c<in[edge[i].b]&&edge[i].a!=edge[i].b)
     38             {
     39                 predge[edge[i].b]=edge[i].a;//找出每个点的最小入弧
     40                 if(edge[i].a==root) ansroot=i;
     41                 in[edge[i].b]=edge[i].c;
     42             }
     43         }
     44         for(int i=0;i<n;i++)
     45         {
     46             if(i==root) continue;
     47             if(in[i]==INT_MAX) return -1;
     48         }
     49         in[root]=0;
     50         int cnt=0;
     51         memset(id,-1,sizeof(id));
     52         memset(visit,-1,sizeof(visit));
     53         for(int i=0;i<n;i++)
     54         {
     55             ret+=in[i];//进行缩圈
     56             v=i;
     57             while(visit[v]!=i&&id[v]==-1&&v!=root)
     58             {
     59                 visit[v]=i;
     60                 v=predge[v];
     61             }
     62             if(v!=root&&id[v]==-1)
     63             {
     64                 for(u=predge[v];u!=v;u=predge[u])
     65                     id[u]=cnt;
     66                 id[v]=cnt++;
     67             }
     68         }
     69         if (cnt==0) break;
     70         for (int i=0;i<n;i++)
     71         if (id[i]==-1) id[i]=cnt++;
     72         for (int i=0;i<m;i++)
     73         {
     74             v=edge[i].b;//进行缩点,重新标记。
     75             edge[i].a=id[edge[i].a];
     76             edge[i].b=id[edge[i].b];
     77             if (edge[i].a!=edge[i].b) edge[i].c-=in[v];
     78         }
     79         n=cnt;
     80         root=id[root];
     81     }
     82     return ret;
     83 }
     84 int main()
     85 {
     86     freopen("2121.txt","r",stdin);
     87     int n,m,m1;
     88     while(scanf("%d%d",&n,&m)!=EOF)
     89     {
     90         int a,b;
     91         int r=0;
     92         m1=m;
     93         for(int i=0;i<m;i++)
     94         {
     95             scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
     96             r+=edge[i].c;
     97         }
     98         r++;
     99         for(int i=0;i<n;i++)
    100         {
    101             edge[m].a=n;
    102             edge[m].b=i;
    103             edge[m].c=r;
    104             m++;
    105         }
    106         int ans=dmst(n,n+1,m);
    107         ansroot-=m1;//最小根对应的标号为i-m1
    108         if(ans==-1||ans>=2*r) printf("impossible
    ");
    109         else printf("%d %d
    ",ans-r,ansroot);
    110         printf("
    ");
    111     }
    112     return 0;
    113 }
    View Code
  • 相关阅读:
    快速生成树协议 RSTP
    VLAN间路由
    二层交换机原理
    网络安全系统之四 PKI体系
    网络安全系统之三 数字证书
    生成树协议 STP
    网络安全系统之二 数字签名
    网络安全系统之一 加密算法
    系统可靠性
    DNS资源记录
  • 原文地址:https://www.cnblogs.com/jcf94/p/4066971.html
Copyright © 2011-2022 走看看