【POJ 3164】【朱刘算法模板】Command Network

Command Network

Time Limit:1000MSMemory Limit:131072K

Total Submissions:13977Accepted:4012

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 integerN(N≤ 100), the number of nodes in the destroyed network, andM(M≤ 104), the number of pairs of nodes between which a wire can be built. The nextNlines each contain an ordered pairxiandyi, giving the Cartesian coordinates of the nodes. Then followMlines each containing two integersiandjbetween 1 andN(inclusive) meaning a wire can be built between nodeiand nodejfor 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.19poor snoopy

Source

, galaxy

最小树形图-朱刘算法。

最小树形图其实就是求有向图的“最小生成树”。

lyd的讲解比较清楚。

大致的算法流程是:

1.除了根结点,为每一个结点找到权值最小的入边

2.如果这些权值最小的入边没有构成环,那么这些入边的和就是答案,输出答案然后退出;

否则先把这些入边的和加入到答案中去

3.缩点

4.修改边权:

注意到在第二步中把所有边权和都加入到答案中去了,可是最后有一些边是不需要的,比如最小树形图中肯定没有环,环上的边至少要删掉一个;环与环之间要有边相连;

因此,,我们把每条边的边权都减去出点的最小入边的权值(如果加上这条边,相当于删除原来的入边)

5.返回1继续执行

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#define M 10000+5#define inf 0x3f3f3f3fusing namespace std;int n,m,pre[M],id[M],v[M];double in[M];struct Point{double x,y;}p[M];struct Edge{int x,y;double v;}e[M];void Zhu_Liu(int root){int idx=n;double ans=0.0;while (idx){for (int i=1;i<=n;i++)in[i]=(double)inf,id[i]=-1,v[i]=-1;for (int i=1;i<=m;i++){if (e[i].v>in[e[i].y]||e[i].x==e[i].y) continue;pre[e[i].y]=e[i].x;in[e[i].y]=e[i].v;}in[root]=0,pre[root]=root;for (int i=1;i<=n;i++){if (in[i]==(double)inf){puts("poor snoopy");return;}ans+=in[i];}idx=0;for (int i=1;i<=n;i++)if (v[i]==-1){int t=i;while (v[t]==-1)v[t]=i,t=pre[t];if (v[t]!=i||t==root) continue;id[t]=++idx;int s;for (s=pre[t];s!=t;s=pre[s])id[s]=idx;}if (!idx) break;for (int i=1;i<=n;i++)if (id[i]==-1) id[i]=++idx;for (int i=1;i<=m;i++){e[i].v-=in[e[i].y];e[i].x=id[e[i].x];e[i].y=id[e[i].y];}n=idx;root=id[root];}printf("%.2f\n",ans);}double Getdis(int a,int b){return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));}int main(){while (scanf("%d%d",&n,&m)!=EOF){for (int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);for (int i=1;i<=m;i++)scanf("%d%d",&e[i].x,&e[i].y),e[i].v=Getdis(e[i].x,e[i].y);Zhu_Liu(1);}return 0;}

人的价值,在遭受诱-惑的一瞬间被决定

【POJ 3164】【朱刘算法模板】Command Network

相关文章:

你感兴趣的文章:

标签云: