HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

Ice_cream’s world II
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 2121

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
 
 
题目大意:n个城市,m条有向边。问你是否存在一个城市,能通向其他所有城市且权值和最大。如果存在多个这样的城市,那么输出城市编号最小的那个。如果不存在,输出impossible。
 
解题思路:不定根的最小树形图。我们考虑建立一个起点,这个起点跟所有其他的顶点连边,权值为城市中的所有边的权值和+1。然后就是用朱刘算法求解了。
 
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long INT;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Edge{
    int from,to;
    int dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
int In[maxn];
int ansidx ;
INT Zhuliu(int root,int n,int m){
    INT ret = 0;
    int u,v;
    while(true){
        for(int i = 0; i < n; i++){
            In[i] =  INF;
        }
        for(int i = 0; i < m; i++){
            Edge &e = edges[i];
            u = e.from; v = e.to;
            if(In[v] > e.dist && u != v){
                pre[v] = u;
                if(u == root){  //记录边的编号,这个编号-m就是点编号,因为我们加边是从顶点从小到大
                    ansidx = i;
                }
                In[v] = e.dist;
            }
        }
        for(int i = 0; i < n; i++){
            if(i == root) continue;
            if(In[i] == INF)
                return -1;
        }
        In[root] = 0;
        int cntcir = 0;
        memset(vis,-1,sizeof(vis));
        memset(ID,-1,sizeof(ID));
        for(int i = 0; i < n; i++){
            ret += In[i];
            v = i;
            while(vis[v]!= i && ID[v] ==-1 &&v != root){
                vis[v] = i;
                v = pre[v];
            }
            if(v != root && ID[v] == -1){
                for(u = pre[v]; u != v; u = pre[u]){
                    ID[u] = cntcir;
                }
                ID[v] = cntcir++;
            }
        }
        if(cntcir == 0){
            break;
        }
        for(int i = 0; i < n; i++){
            if(ID[i]==-1){
                ID[i] = cntcir++;
            }
        }
        for(int i = 0; i < m; i++){
            v = edges[i].to;
            Edge & e = edges[i];
            e.from = ID[e.from];
            e.to = ID[e.to];
            if(e.from != e.to){
                e.dist -= In[v];
            }
        }
        n = cntcir;
        root = ID[root];
    }
    return ret;
}
int main(){
    int n,m, T, cas = 0;
    while(scanf("%d%d",&n,&m)!=EOF){
        int a,b,c;
        INT sumd = 0;
        for(int i = 0; i < m; i++){
            scanf("%d%d%d",&a,&b,&c);
            a++,b++;
            edges[i].from = a;
            edges[i].to = b;
            if(a == b){
                edges[i].dist = INF;
                continue;
            }
            edges[i].dist = c;
            sumd += c;
        }
        for(int i = 0; i < n;i++){
            edges[m+i].from = 0;
            edges[m+i].to = i + 1;
            edges[m+i].dist = sumd + 1;
        }
        INT res = Zhuliu(0,n+1,m+n);
        if(res == -1||res > 2*sumd+1){
            puts("impossible");
        }else{
            printf("%lld %d\n",res - sumd -1,ansidx - m);
        }puts("");
    }
    return 0;
}

/*
3 2
0 1 2
1 2 3

3 0

*/

  

 

转载于:https://www.cnblogs.com/chengsheng/p/4933258.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值