有向图最小生成树无定根 并要求求出根的位置

Ice_cream’s world II

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


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 1
0 0 2
10 1 3
20 2 3 30
 

 

Sample Output
impossible
 
40 0
 
 

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
const int MAXN = 1e3 + 10;
typedef long long LL;
int id[MAXN] , vis[MAXN] , pre[MAXN] , pos;
LL INF = 1e17 , d[MAXN];
struct node {
    int u , v , cost;
}edge[MAXN * MAXN];

LL zhuliu(int root , int V , int E) {//0,n+1,n+m
    LL res = 0;
    while(true) {
        for(int i = 0 ; i < V ; i++) {
            d[i] = INF;
        }
        for(int i = 0 ; i < E ; i++) {
            int u = edge[i].u , v = edge[i].v;
            if(u != v && d[v] > edge[i].cost) {
                d[v] = edge[i].cost;
                pre[v] = u;
                if(u == root) {
                    pos = i;    //记录位置    除了这里不一样 其他地方都是朱刘算法的模板
                }
            }
        }
        for(int i = 0 ; i < V ; i++) {
            if(d[i] == INF && i != root) {
                return -1;
            }
        }
        int cont = 0;
        memset(id , -1 , sizeof(id));
        memset(vis , -1 , sizeof(vis));
        d[root] = 0;
        for(int i = 0 ; i < V ; i++) {
            int v = i;
            res += d[i];
            while(id[v] == -1 && vis[v] != i && v != root) {
                vis[v] = i;
                v = pre[v];
            }
            if(id[v] == -1 && v != root) {
                for(int u = pre[v] ; u != v ; u = pre[u]) {
                    id[u] = cont;
                }
                id[v] = cont++;
            }
        }
        if(!cont) {
            break;
        }
        for(int i = 0 ; i < V ; i++) {
            if(id[i] == -1) {
                id[i] = cont++;
            }
        }
        for(int i = 0 ; i < E ; i++) {
            int u = edge[i].u , v = edge[i].v;
            edge[i].u = id[u];
            edge[i].v = id[v];
            if(id[u] != id[v]) {
                edge[i].cost -= d[v];
            }
        }
        V = cont;
        root = id[root];
    }
    return res;
}

int main()
{
    int n , m;
    while(~scanf("%d %d" , &n , &m)) {
        LL sum = 0;
        for(int i = 0 ; i < m ; i++) {
            scanf("%d %d %d" , &edge[i].u , &edge[i].v , &edge[i].cost);
            edge[i].u++ , edge[i].v++;
            sum += edge[i].cost;
        }
        sum++;   //边权大于总权值
        for(int i = m ; i < n + m ; i++) {
            edge[i].u = 0;    //0为虚拟节点
            edge[i].v = i - m + 1;
            edge[i].cost = sum;
        }
        LL res = zhuliu(0 , n + 1 , n + m);   //n + 1 个点  n + m 条边
        if(res == -1 || res - sum >= sum) {    //要是res - sum < sum 的话就说明 0的出度为1  说明原图是连通图
            printf("impossible\n\n");
        }
        else {
            printf("%lld %d\n\n" , res - sum , pos - m);
        }
    }
}

posted @ 2017-07-29 18:26  Billyshuai  阅读(244)  评论(0编辑  收藏  举报