zoukankan      html  css  js  c++  java
  • Ice_cream’s world II(最小树形图,加虚点)

    Ice_cream’s world II

    http://acm.hdu.edu.cn/showproblem.php?pid=2121

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6736    Accepted Submission(s): 1779


    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
     
    Author
    Wiskey
     
    Source
     
    不懂为什么用了注释中的代码会WA....
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <string>
      7 #include <queue>
      8 const int INF=0x3f3f3f3f;
      9 #define MAXN 1002
     10 #define MAXM 10005
     11 using namespace std;
     12 struct Edge{
     13     int u,v;
     14     int cost;
     15 }edge[MAXM];
     16 
     17 int pre[MAXN],id[MAXN],visit[MAXN];
     18 int in[MAXN];
     19 int minroot;
     20 
     21 int zhuliu(int root,int n,int m){
     22     int res=0;
     23     while(1){
     24         for(int i=0;i<n;i++){
     25             in[i]=INF;
     26         }
     27         for(int i=0;i<m;i++){
     28             if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v]){
     29                 pre[edge[i].v]=edge[i].u;
     30                 in[edge[i].v]=edge[i].cost;
     31                 if(edge[i].u==root){
     32                     minroot=i;
     33                 }
     34             }
     35         }
     36         for(int i=0;i<n;i++){
     37             if(i!=root && in[i]==INF){
     38                 return -1;
     39             }
     40         }
     41         int tn=0;
     42         memset(id,-1,sizeof(id));
     43         memset(visit,-1,sizeof(visit));
     44         in[root]=0;
     45         for(int i=0;i<n;i++){
     46             res+=in[i];
     47             int v=i;
     48             while(visit[v]!=i&&id[v]==-1&&v!=root){
     49                 visit[v]=i;
     50                 v=pre[v];
     51             }
     52             if(v!=root&&id[v]==-1){
     53                 for(int u=pre[v];u!=v;u=pre[u]){
     54                     id[u]=tn;
     55                 }
     56                 id[v]=tn++;
     57             }
     58         }
     59         if(tn==0) break;
     60         for(int i=0;i<n;i++){
     61             if(id[i]==-1){
     62                 id[i]=tn++;
     63             }
     64         }
     65        /* for(int i=0;i<m;){
     66             int v=edge[i].v;
     67             edge[i].u=id[edge[i].u];
     68             edge[i].v=id[edge[i].v];
     69             if(edge[i].u!=edge[i].v){
     70                 edge[i++].cost-=in[v];
     71             }
     72             else{
     73                 swap(edge[i],edge[--m]);
     74             }
     75         }*/
     76         for(int i=0;i<m;i++){
     77             int v=edge[i].v;
     78             edge[i].u=id[edge[i].u];
     79             edge[i].v=id[edge[i].v];
     80             if(edge[i].u!=edge[i].v){
     81                 edge[i].cost-=in[v];
     82             }
     83         }
     84         n=tn;
     85         root=id[root];
     86     }
     87     return res;
     88 }
     89 
     90 
     91 int main(){
     92     int n,m;
     93     while(~scanf("%d %d",&n,&m)){
     94         int sum=0;
     95         for(int i=0;i<m;i++){
     96             scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].cost);
     97             edge[i].u++;
     98             edge[i].v++;
     99             sum+=edge[i].cost;
    100         }
    101         sum++;
    102         for(int i=m;i<n+m;i++){
    103             edge[i].u=0,edge[i].v=i-m+1,edge[i].cost=sum;
    104         }
    105         int ans=zhuliu(0,n+1,m+n);
    106         if(ans==-1||ans>=2*sum){
    107             puts("impossible");
    108         }
    109         else{
    110             printf("%d %d
    ",ans-sum,minroot-m);
    111         }
    112         printf("
    ");
    113     }
    114 }
    View Code
  • 相关阅读:
    在阿里写了8年代码后,我才明白这些道理
    2017双11交易系统TMF2.0技术揭秘,实现全链路管理
    加入新公司快速进入状态的心得
    Kibana+ElasticSearch实现索引数据的几种查询方式
    记一次jenkins发生的无法判断字符串前后空格
    ansible-playbook调试
    记一次ansible-playbook jenkins传空格的标量导致删除了服务的主目录
    rabbitmq集群中队列的完整性
    html5分割上传实现超大文件无插件网页上传思路
    html5分割上传实现超大文件无插件网页上传功能
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9995222.html
Copyright © 2011-2022 走看看