• 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;
    }
  • 相关阅读:
    selenium 模拟键盘事件 复制粘贴、右键、回车等
    02安卓用户界面优化之(二)SlidingMenu使用方法
    02Android用户界面优化之(一)Android Fragment
    (九)Android权限系统
    Android SDK 在线更新镜像服务器资源
    (八)Android广播接收器BroadcastReceiver
    (七)Android中AIDL的应用与理解
    (六)Android中Service通信
    (五)认识Android中的Service
    Gradle中文乱码
  • 原文地址:https://www.cnblogs.com/fenice/p/5641845.html
Copyright © 2020-2023  润新知