tju 2248 Channel Design 有固定根的最小树形图

We need irrigate our farms, but there is only one source of water nearby. So we need build some water channels with minimum cost.

In Figure (a), V1 indicates the source of water. Other N-1 nodes in the Figure indicate the farms we need to irrigate. An edge represents you can build a channel between the two nodes, to irrigate the target. The integers indicate the cost of a channel between two nodes.

Figure (b) represents a design of channels with minimum cost.

Input

There are multiple cases, the first line of each case contains two integers N and M (2 ≤ N ≤ 100; 1 ≤ M ≤ 10000), N shows the number of nodes. The following M lines, each line contains three integers i j cij, means we can build a channel from node Vi to node Vj, which cost cij. (1 ≤ ij ≤ Ni ≠ j; 1 ≤ cij ≤ 100)

The source of water is always V1.
The input is terminated by N = M = 0.

Output

For each case, output a single line contains an integer represents the minimum cost.

If no design can irrigate all the farms, output "impossible" instead.

Sample Input

5 8
1 2 3
1 3 5
2 4 2
3 1 5
3 2 5
3 4 4
3 5 7
5 4 3
3 3
1 2 3
1 3 5
3 2 1
0 0

Sample Output

17
6


//这个比较快

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 1501;
const int TOOBIG = (1<<30);
//1是固定根
/*
 if(!min_shuxingtu()) puts("impossible");
else{
    printf("%.0lf\n",ans);
}
*/

//不能将!=-1改成>0,==-1改成<0
double g[maxn][maxn], mincost[maxn];
int pre[maxn], n;
bool vis[maxn], exist[maxn];
double  ans;


void combine(int *list, int h, int r) {
    // 环中的点依序保存在 list[h..r] 中,将这个环缩成一个点
    memset(vis, 0, sizeof(vis));
    for (int i = h; i <= r; i++) {
        vis[list[i]] = true;
        exist[list[i]] = false;
    }
    int now = list[h];
    // 首先处理边权问题
    double newg[maxn];
    for (int j = 1; j <= n; j++) newg[j] = TOOBIG;
    for (int i = h; i <= r; i++) {
        int x = list[i];
        ans += mincost[x];
        for (int j = 1; j <= n; j++) if (!vis[j] && exist[j]) {
            if (g[j][x] != -1) {
                double tmp = g[j][x] - mincost[x];
                newg[j] = min(newg[j], tmp);
            }
            if ((g[x][j] != -1 && g[x][j] < g[now][j]) || g[now][j] == -1) g[now][j] = g[x][j];
        }
    }
    exist[now] = true;
    for (int j = 1; j <= n; j++) g[j][now] = newg[j];
    // 然后处理缩成的点引出的最小边
    for (int i = 2; i <= n; i++) if (exist[i] && !vis[i] && vis[pre[i]]) pre[i] = now;
    // 最后处理缩成的点自己的最小边
    mincost[now] = TOOBIG;
    for (int i = 1; i <= n; i++)
        if (exist[i] && i != now && g[i][now] != -1 && g[i][now] < mincost[now]) {
            mincost[now] = g[i][now];
            pre[now] = i;
        }
}


bool find_circle(int *list, int &h, int &r) {
    // 由于每个点的 vis 只会被标记一次,所以这个过程是 O(n) 的
    // 如果找到了环那么将环中的点依序保存在 list[h..r] 中
    int mark[maxn];
    memset(vis, 0, sizeof(vis));
    for (int k = 2; k <= n; k++) if (!vis[k] && exist[k]) {
        memset(mark, 0, sizeof(mark));
        r = 0;
        int i = k;
        for (; i != 1 && !mark[i] && !vis[i]; i = pre[i]) {
            vis[i] = true;
            r++;
            list[r] = i;
            mark[i] = r;
        }
        if (mark[i]) {
            h = mark[i];
            return true;
        }
    }
    return false;
}


void dfs(int v){
    vis[v] = true;
    for (int i = 1; i <= n; i++) if (!vis[i] && g[v][i] != -1) dfs(i);
}


bool min_shuxingtu() {
    // 求以 1 为根的最小树形图,原图以邻接矩阵保存于 g[1..n][1..n] 中,求解将破坏 g 矩阵
    // 如果存在返回 true,并且将最小树形图的边权和放在 ans 中,否则返回 false
    memset(vis, 0, sizeof(vis));
    dfs(1);
    for (int i = 1; i <= n; i++) if (!vis[i]) return false;
    // 初始化 mincost 和 pre 和 id
    for (int i = 1; i <= n; i++) exist[i] = true;
    for (int i = 2; i <= n; i++) {
        mincost[i] = TOOBIG;
        for (int j = 1; j <= n; j++)
            if (j != i && g[j][i] != -1 && g[j][i] < mincost[i]) {
                mincost[i] = g[j][i];
                pre[i] = j;
            }
    }
    ans = 0;
    int list[maxn], h, r;
    while (find_circle(list, h, r)) combine(list, h, r);
    for (int i = 2; i <= n; i++) if (exist[i]) ans += mincost[i];
    return true;
}


int main(){
    int i,j,u,v,m;
    while(scanf("%d %d",&n,&m)!=EOF&&n){
        for(i=0;i<=n;i++) for(j=0;j<=n;j++) g[i][j]=-1;
        for(i=1;i<=m;i++)
        {
            double t;
            scanf("%d%d%lf",&u,&v,&t);
            g[u][v]=t;
        }
        if(!min_shuxingtu()) puts("impossible");
        else{
            printf("%.0lf\n",ans);
        }
    }
    return 0;
}
//对于不固定根的最小树形图,新加一个点,和每个点连权相同的边,
//这个权大于原图所有边权的和,这样这个图固定跟的最小树形图和原图
//不固定跟的最小树形图就是对应的了。

//这个比较慢


#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
//1是固定根
/*使用方法
if(!connected()) puts("NO");
else{
    min_arborescence();
    printf("%lf\n",ans);
}
*/
const int MAXN = 110;
const int INF = 0x7FFFFFFF;
int n,m,pre[MAXN];
bool circle[MAXN],visit[MAXN];
//ans初始化为0
//map[i][j]初始化为INF,map[i][j]表示i->j有边和权
double ans,map[MAXN][MAXN];
void dfs(int u){
    visit[u]=true;
    for(int i=2;i<=n;i++)
        if(!visit[i] && map[u][i]!=INF)
            dfs(i);
}
bool connected(){
    memset(visit,false,sizeof(visit));
    int i,cnt=0;
    for(i=1;i<=n;i++)
        if(!visit[i])
            dfs(i),cnt++;
    return cnt==1 ? true : false;
}
void min_arborescence(){
    int i,j,k;
    memset(circle,false,sizeof(circle));
    while(true){
        for(i=2;i<=n;i++){
            if(circle[i]) continue;
            pre[i]=i;
            map[i][i]=INF;
            for(j=1;j<=n;j++){
                if(circle[j]) continue;
                if(map[j][i]<map[pre[i]][i])
                    pre[i]=j;
            }
        }
        for(i=2;i<=n;i++){
            if(circle[i]) continue;
            j=i;
            memset(visit,false,sizeof(visit));
            while(!visit[j] && j!=1){
                visit[j]=true;
                j=pre[j];
            }
            if(j==1) continue;
            i=j;
            ans+=map[pre[i]][i];
            for(j=pre[i];j!=i;j=pre[j]){
                ans+=map[pre[j]][j];
                circle[j]=true;
            }
            for(j=1;j<=n;j++){
                if(circle[j]) continue;
                if(map[j][i]!=INF)
                    map[j][i]-=map[pre[i]][i];
            }
            for(j=pre[i];j!=i;j=pre[j])
                for(k=1;k<=n;k++){
                    if(circle[k]) continue;
                    map[i][k]=min(map[i][k],map[j][k]);
                    if(map[k][j]!=INF)
                        map[k][i]=min(map[k][i],map[k][j]-map[pre[j]][j]);
                }
            break;
        }
        if(i>n){
            for(j=2;j<=n;j++){
                if(circle[j]) continue;
                ans+=map[pre[j]][j];
            }
            break;
        }
    }
}


int main(){
    int i,j,u,v;
    while(scanf("%d %d",&n,&m)!=EOF&&n){
        for(ans=i=0;i<=n;i++) for(j=0;j<=n;j++) map[i][j]=INF;
        for(i=1;i<=m;i++)
        {
            double t;
            scanf("%d%d%lf",&u,&v,&t);
            map[u][v]=t;
        }
        if(!connected()) puts("impossible");
        else{
            min_arborescence();
            printf("%.0lf\n",ans);
        }
    }
    return 0;
}
//对于不固定根的最小树形图,新加一个点,和每个点连权相同的边,
//这个权大于原图所有边权的和,这样这个图固定跟的最小树形图和原图
//不固定跟的最小树形图就是对应的了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值