poj 3164 Command Network(最小樹形圖模板題)朱_ 劉演算法
http://poj.org/problem?id=3164
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 12454 | Accepted: 3602 |
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題目大意:給定n個點的座標和m組能直接相互到達的點對(有向),權值為兩點的距離,求最小樹形圖。1為根節點
思路:朱劉演算法模板題。
#include <cstdio>
#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;
//======================================================================
//最小樹形圖(有向圖的最小生成樹)朱劉演算法模板
const int N=101,M=10001,inf=2147483647;
struct edge
{
int u,v;
double w;
} e[M];
struct node
{
double x,y;
} p[N];
int n,m,id[N],pre[N],v[N];
double inw[N];
double ans;
void zhu_liu(int root)
{
int s,t,idx=n;
ans=0;
while (idx)
{
for (int i=1; i<=n; ++i) inw[i]=inf,id[i]=-1,v[i]=-1;
for (int i=1; i<=m; ++i)
{
s=e[i].u;
t=e[i].v;
if (e[i].w>inw[t] || s==t) continue;
pre[t]=s;
inw[t]=e[i].w;
}
inw[root]=0;
pre[root]=root;
for (int i=1; i<=n; ++i)
{
if (inw[i]==inf)
{
printf("poor snoopy\n");
return;
}
ans+=inw[i];
}
idx=0;
for (int i=1; i<=n; ++i)
if (v[i]==-1)
{
t=i;
while (v[t]==-1) v[t]=i,t=pre[t];
if (v[t]!=i || t==root) continue;
id[t]=++idx;
for (s=pre[t]; s!=t; s=pre[s]) id[s]=idx;
}
if (idx==0) continue;
for (int i=1; i<=n; ++i)
if (id[i]==-1) id[i]=++idx;
for (int i=1; i<=m; ++i)
{
e[i].w-=inw[e[i].v];
e[i].u=id[e[i].u];
e[i].v=id[e[i].v];
}
n=idx;
root=id[root];
}
printf("%.2lf\n",ans);
}
//==============================================================
double longth(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
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].u,&e[i].v);
e[i].w=(longth(p[e[i].u],p[e[i].v]));
}
zhu_liu(1);
}
return 0;
}
相關文章
- 最小樹形圖(朱劉演算法)演算法
- 最小樹形圖(有向圖的最小生成樹)朱劉演算法演算法
- 【模板】最小生成樹
- 【模板】最小生成樹-kruskal
- 演算法-圖論-最小生成樹演算法圖論
- P6192 【模板】最小斯坦納樹 題解
- POJ 2486 Apple Tree(樹形dp)APP
- 圖論中的最小生成樹演算法圖論演算法
- POJ 3107 Godfather(樹形dp)Go
- POJ3107Godfather[樹形DP 樹的重心]Go
- 【樹形dp】poj 1947 Rebuilding RoadsRebuild
- 前端必會演算法 - 最小生成樹問題前端演算法
- 演算法 最小高度樹演算法
- 【圖論】最小生成樹圖論
- 圖論 最小生成樹圖論
- 【JAVA演算法】圖論演算法 --求最小生成樹Prim演算法Java演算法圖論
- poj2486Apple Tree[樹形揹包!!!]APP
- 最小生成樹__Kurskal演算法演算法
- 最小生成樹__Prim演算法演算法
- 最小生成樹的演算法演算法
- Kruskal 最小生成樹演算法演算法
- 圖論 最小生成樹問題(最優連線問題)圖論
- Prim 最小生成樹 圖解圖解
- POJ 3565 Ants (最小權完美匹配 KM演算法)演算法
- POJ2031 Building a Space Station(最小生成樹,prim)UI
- 演算法題:三角形的最小路徑和演算法
- 最小生成樹之 Prim 演算法演算法
- 【演算法學習】最小生成樹演算法
- 演算法與資料結構之帶權圖與圖最小生成樹演算法資料結構
- C語言圖形設計 劉振安pdfC語言
- POJ 1947 Rebuilding Roads(基礎的樹形dp)Rebuild
- POJ 3469-Dual Core CPU(Dinic 最大流/最小割演算法)演算法
- 最小生成樹——Prim演算法和Kruscal演算法演算法
- 最小生成樹:Kruskal演算法和Prim演算法演算法
- 最小生成樹-Prim演算法和Kruskal演算法演算法
- 樹形問題選講
- POJ 1734 Sightseeing trip Floyd求無向圖最小環
- POJ 3253-Fence Repair(哈夫曼樹-最小值優先佇列)AI佇列