zoukankan      html  css  js  c++  java
  • POJ 3164 Command Network 最小树形图

    题目链接:

    题目

    Command Network
    Time Limit: 1000MS
    Memory Limit: 131072K

    问题描述

    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.

    输入

    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.

    输出

    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’.

    样例

    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

    output
    31.19
    poor snoopy

    题意

    求固定根的最小树形图

    题解

    朱刘算法

    代码

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    const int maxn = 111;
    const int maxm = 11111;
    const double INF = ~0u >> 1;
    
    struct Edge {
    	int u, v; 
    	double w;
    }egs[maxm];
    
    struct Point {
    	int x, y;
    }pt[maxn];
    
    int n, m;
    
    double dis(const Point& p1, const Point& p2) {
    	return sqrt(1.0*(p1.x - p2.x)*(p1.x - p2.x) + 1.0*(p1.y - p2.y)*(p1.y - p2.y));
    }
    
    double in[maxn];
    int id[maxn], vis[maxn], pre[maxn];
    double Directed_MST(int rt) {
    	double ret = 0;
    	while (1) {
    		//求最小入度边
    		for (int i = 0; i < n; i++) in[i] = INF;
    		for (int i = 0; i < m; i++) {
    			Edge& e = egs[i];
    			if (e.w < in[e.v] && e.u != e.v) {
    				in[e.v] = e.w;
    				pre[e.v] = e.u;
    			}
    		}
    		for (int i = 0; i < n; i++) {
    			if (i!=rt&&in[i] == INF) return -1;
    		}
    		int tot = 0;
    		memset(id, -1, sizeof(id));
    		memset(vis, -1, sizeof(vis));
    		in[rt] = 0;
    		//找环,缩点
    		for (int i = 0; i < n; i++) {
    			ret += in[i];
    			int v = i;
    			while (vis[v] != i&&id[v] == -1 && v != rt) {
    				vis[v] = i;
    				v = pre[v];
    			}
    			if (id[v] == -1 && v != rt) {
    				for (int u = pre[v]; u != v; u = pre[u]) {
    					id[u] = tot;
    				}
    				id[v] = tot++;
    			}
    		}
    		//没有环
    		if (tot == 0) break;
    		for (int i = 0; i < n; i++) {
    			if (id[i] == -1) id[i] = tot++;
    		}
    		//更新到环的距离
    		for (int i = 0; i < m; i++) {
    			Edge& e = egs[i];
    			int v = e.v;//这个v要留下来!
    			e.u = id[e.u],e.v = id[e.v];
    			if (e.u != e.v) {
    				e.w -= in[v];
    			}
    		}
    		n = tot;
    		rt = id[rt];
    	}
    	return ret;
    }
    
    int main() {
    	while (scanf("%d%d", &n, &m) == 2 && n) {
    		for (int i = 0; i < n; i++) {
    			scanf("%d%d", &pt[i].x, &pt[i].y);
    		}
    		for (int i = 0; i < m; i++) {
    			Edge& e = egs[i];
    			scanf("%d%d", &e.u, &e.v),e.u--,e.v--;
    			if (e.u != e.v) e.w = dis(pt[e.u], pt[e.v]);
    			else e.w = INF;
    		}
    		double ans = Directed_MST(0);
    		if (ans ==-1) {
    			puts("poor snoopy");
    		}
    		else {
    			printf("%.2f
    ", ans);
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    document.all还是document.getElementsByName
    B/S架构下软件开发技术参考
    "未将对象引用设置到对象的实例"异常的原因,请大家接下去 1、ViewState 对象为Unll。
    爱晚红枫的博客配色绿野仙踪
    适用于.text系统的博客皮肤Nature和purple
    XML数据岛,数据绑定
    【收藏①】17种正则表达式
    如何去除字符串中的多余空格?
    在NTFS分区中复制文件的同时如何复制权限
    我的后大学时代
  • 原文地址:https://www.cnblogs.com/fenice/p/5641845.html
Copyright © 2011-2022 走看看