POJ 3164Command Network

来源:互联网 发布:php环境配置教程 百度 编辑:程序博客网 时间:2024/05/16 15:48

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 60 64 60 07 201 21 32 33 43 13 24 30 01 00 11 21 34 12 3

Sample Output

31.19

poor snoopy

最小树形图,朱刘算法模板。

#include<cmath>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define rep(i,j,k) for(int i=j;i<=k;i++)#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])#define inone(x) scanf("%d",&x)#define intwo(x,y) scanf("%d%d",&x,&y)#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)const int N = 1e5+ 10;const double INF = 1e18;int n, m, pre[N], vis[N], f[N];double x[N], y[N], ans;double in[N];struct point{int x, y;double d;}a[N];double dis(int i, int j){return sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));}double zhuliu(int rt){double res = 0;while (true){rep(i, 1, n) in[i] = INF;rep(i, 1, m){int x = a[i].x, y = a[i].y;if (a[i].d < in[y] && x != y){pre[y] = x; in[y] = a[i].d;}}rep(i, 1, n){if (i == rt) in[rt] = 0;if (in[i] == INF) return -1;}int cnt = 1;rep(i, 1, n) vis[i] = f[i] = -1;rep(i, 1, n){res += in[i];int j = i;while (j != rt&&vis[j] != i&&f[j] == -1){vis[j] = i; j = pre[j];}if (j != rt&&f[j] == -1){for (int k = pre[j]; k != j; k = pre[k]) f[k] = cnt;f[j] = cnt++;}}if (cnt == 1) break;rep(i, 1, n) if (f[i] == -1) f[i] = cnt++;rep(i, 1, m){int x = a[i].x, y = a[i].y;a[i].x = f[x]; a[i].y = f[y];if (f[x] != f[y]) a[i].d -= in[y];}n = cnt - 1; rt = f[rt];}return res;}int main(){while (intwo(n, m) != EOF){rep(i, 1, n) scanf("%lf%lf", &x[i], &y[i]);rep(i, 1, m){intwo(a[i].x, a[i].y);a[i].d = a[i].x - a[i].y ? dis(a[i].x, a[i].y) : INF;}if ((ans = zhuliu(1)) == -1) printf("poor snoopy\n");else printf("%.2lf\n", ans);}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米手机云储存空间不足怎么办 路由器被黑了打不开网页怎么办 致人轻伤跑了怎么办 轻伤对方要30万怎么办 老公用老婆的钱怎么办 想注册个公司要怎么办 域名续费不知道找谁怎么办 代收快递弄丢了怎么办 货到付款的快递人不在怎么办 快递送货上门人不在怎么办 ems快递签收人不在怎么办 快递被别人取了怎么办 怎么办快递宗和收发点 快递电话写错了怎么办 网上买沙发想退货怎么办 买的电脑想退货怎么办 买了衣服想退货怎么办 天猫买药审核通过后不要了怎么办 京东维修无发票怎么办 京东维修没有发票怎么办 苹果6s外音没了怎么办 苹果6splus开不了机怎么办 顺丰快递寄件填错收件人地址怎么办 收快递电话换了怎么办 顺丰快递没人收怎么办 网购东西没收到怎么办 中通快递没收到怎么办 快递员不给验货怎么办 顺丰验货不要了怎么办 闲鱼买家掉包了怎么办 闲鱼正在退款中怎么办 拒收货物卖家拒绝退款怎么办 货物没问题淘宝卖家拒收怎么办 头发稀少长的慢怎么办 没满16岁怎么办银行卡 网上买东西手机号填错了怎么办 买东西电话号码填错了怎么办 淘宝联盟扣54分怎么办 联盟被扣54分怎么办 ofo押金退了余额怎么办 网购还没收货就已签收怎么办