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
此题考察最小树形图的应用。
在开始时先用深搜遍历一遍,判断是否所有点都能达到,若不能,则输出"poor snoopy"。
首先找到除根以外的所有节点的最小入边,若构成环,则将其缩为一个点,并修改相关边。
最后,只需要再统计出所尊不在环上的点的入边值即可。
Accode:
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define sqr(x) ((x) * (x))
#define dist(u, v) (sqrt(sqr(x[u] - x[v]) + sqr(y[u] - y[v])))
const int maxN = 110;
const double INF = 1e198;
bool marked[maxN], circle[maxN];
double mp[maxN][maxN], x[maxN], y[maxN], ans;
int pre[maxN], n, m, root;
void Dfs(int u)
{
marked[u] = 1;
for (int v = 1; v < n + 1; ++v)
if (!marked[v] && mp[u][v] < INF)
Dfs(v);
return;
}
inline bool ok()
{
memset(marked, 0, sizeof marked);
Dfs(root = 1);
for (int i = 1; i < n + 1; ++i)
if (!marked[i]) return 0;
return 1;
}
inline int loop()
{
pre[root = 1] = 1;
for (int i = 1; i < n + 1; ++i)
if (!circle[i] && i - root)
{
mp[i][i] = INF; pre[i] = i;
//排除自环(自环肯定不在最小树形图上)。
for (int j = 1; j < n + 1; ++j)
if (!circle[j] && mp[j][i] < mp[pre[i]][i])
pre[i] = j;
//找到i的入边。
}
for (int i = 1; i < n + 1; ++i)
if (!circle[i])
{
memset(marked, 0, sizeof marked);
int j = i;
for (; !marked[j]; j = pre[j]) marked[j] = 1;
if (j == root) continue;
return j; //这里是j遇到了环而不是i,所以应该返回j。
}
return -1;
} //检查是否有环。
inline void update(int u)
{
ans += mp[pre[u]][u];
for (int j = pre[u]; j - u; j = pre[j])
ans += mp[pre[j]][j], circle[j] = 1;
//累加环上的边,并把环上的节点除u外全部作上标记。
for (int i = 1; i < n + 1; ++i)
if (!circle[i] && mp[i][u] < INF)
mp[i][u] -= mp[pre[u]][u];
//这句话的含义是若添加了边<i,u>,则不需要边u的入边
//(因为此时通过i可以到达u,则不需要其入边。
for (int j = pre[u]; j - u; j = pre[j])
for (int i = 1; i < n + 1; ++i)
if (!circle[i])
{
if (mp[i][j] < INF)
mp[i][u] = std::min(mp[i][u], mp[i][j] - mp[pre[j]][j]);
mp[u][i] = std::min(mp[u][i], mp[j][i]);
} //缩点,并把环上所有点都对u做一次松弛。
return;
}
inline void solve()
{
memset(circle, 0, sizeof circle); //注意清零。
int j;
while ((j = loop()) + 1) update(j);
for (int i = 1; i < n + 1; ++i)
if (!circle[i] && i - root)
ans += mp[pre[i]][i];
//累加剩余的入边。
printf("%.2lf\n", ans);
return;
}
int main()
{
freopen("Command_Network.in", "r", stdin);
freopen("Command_Network.out", "w", stdout);
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = 0; i < n + 1; ++i)
for (int j = 0; j < n + 1; ++j)
mp[i][j] = INF;
//注意mp数组的初始化。
for (int i = 1; i < n + 1; ++i)
scanf("%lf%lf", x + i, y + i);
while (m--)
{
int u, v;
scanf("%d%d", &u, &v);
mp[u][v] = dist(u, v);
}
ans = 0;
if (!ok()) printf("poor snoopy\n");
else solve();
}
return 0;
}