poj 3164 Command Network 有固定根的最小树形图

Description

After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project.

With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy’.

Sample Input

4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3

Sample Output

31.19
poor snoopy


//这个模板比较快

#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;
}


double x[maxn],y[maxn];
inline double distance(double x1,double y1,double x2,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main(){
    int i,j,u,v,m;
    while(scanf("%d %d",&n,&m)!=EOF){
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=n;j++) g[i][j]=-1;
        }
        for(i=1;i<=n;i++) scanf("%lf %lf",&x[i],&y[i]);
        while(m--){
            scanf("%d %d",&u,&v);
            g[u][v]=distance(x[u],y[u],x[v],y[v]);
        }
        if(!min_shuxingtu()) puts("poor snoopy");
        else{
            printf("%.2lf\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;
        }
    }
}
double x[MAXN],y[MAXN];
inline double distance(double x1,double y1,double x2,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main(){
    int i,j,u,v;
    while(scanf("%d %d",&n,&m)!=EOF){
        for(ans=i=0;i<=n;i++) for(j=0;j<=n;j++) map[i][j]=INF;
        for(i=1;i<=n;i++) scanf("%lf %lf",&x[i],&y[i]);
        while(m--){
            scanf("%d %d",&u,&v);
            map[u][v]=distance(x[u],y[u],x[v],y[v]);
        }
        if(!connected()) puts("poor snoopy");
        else{
            min_arborescence();
            printf("%.2lf\n",ans);
        }
    }
    return 0;
}
//对于不固定根的最小树形图,新加一个点,和每个点连权相同的边,
//这个权大于原图所有边权的和,这样这个图固定跟的最小树形图和原图
//不固定跟的最小树形图就是对应的了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值