1. 程式人生 > >poj 3164 Command Network 【最小樹形圖——朱-劉演算法】

poj 3164 Command Network 【最小樹形圖——朱-劉演算法】

                         **Command Network**
            Time Limit: 1000MS      Memory Limit: 131072K
            Total Submissions: 17881        Accepted: 5126

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

題目大意:在戰爭時期,資訊的傳遞變得尤為重要。其中一個國家想在各個戰地之間建立通訊線路,由於敵人的阻斷, 一些戰地之間無法進行通訊,司令希望以最少的電線連線各個戰地,以便能夠儘快實現通訊。現在請你寫個程式,判斷是否存在這樣的線路, 如果存在,輸出所需的最少的電線數

AC程式碼:

# include <cstdio>
# include <cstring>
# include <math.h>

# define MAXN 105 * 1000
# define INF 10000000.0

struct POS
{
    double x;
    double y;
};

struct POINT
{
    int start;
    int end;
    double dist;
};

POS ps[MAXN];
POINT pt[MAXN];
double in[MAXN];
int pre[MAXN];
int vis[MAXN];
int Node[MAXN];

double getDist(POS a, POS b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

void Directed_MST(int node, int nv, int ne)
{
    int i;

    double sum = 0.0;
    while (true)
    {
        //找結點最小入邊
        for (i = 1; i <= nv; i++)
        {
            in[i] = INF;
        }
        for (i = 1; i <= ne; i++)
        {
            int start = pt[i].start;
            int end = pt[i].end;
            if (pt[i].dist < in[end] && start != end)
            {
                pre[end] = start; //記錄前驅結點
                in[end] = pt[i].dist;
            }
        }
        //判斷是否能夠構造成為最小生成樹
        for (i = 1; i <= nv; i++)
        {
            if (i == node)
            {
                continue;
            }
            if (in[i] == INF)
            {
                printf("poor snoopy\n");//不能構造
                return;
            }
        }

        //找環
        int cntnode = 1;
        memset(vis, 0, sizeof(vis));
        memset(Node, -1, sizeof(Node));
        in[node] = 0;
        for (i = 1; i <= nv; i++)
        {
            sum += in[i];
            int v = i;
            while (vis[v] != i && v != node)
            {
                vis[v] = i;
                v = pre[v];
            }
            if (v != node && -1 == Node[v])
            {
                for (int u = pre[v]; u != v; u = pre[u])
                {
                    Node[u] = cntnode;
                }
                Node[v] = cntnode++;
            }
        }

        if (cntnode == 1) //無環
        {
            break;
        }
        for (i = 1; i <= nv; i++)
        {
            if (-1 == Node[i])
            {
                Node[i] = cntnode++;
            }
        }

        //收縮 構造新的樹
        for (i = 1; i <= ne; i++)
        {
            int v = pt[i].end;
            pt[i].start = Node[pt[i].start];
            pt[i].end = Node[pt[i].end];
            if (pt[i].start != pt[i].end)
            {
                pt[i].dist -= in[v];
            }
        }
        nv = cntnode - 1;
        node = Node[node];
    }
    printf("%.2lf\n", sum);
}

int main(void)
{
    int n, m;
    int i;
    while (~scanf("%d %d", &n, &m))
    {
        for (i = 1; i <= n; i++)
        {
            scanf("%lf %lf", &ps[i].x, &ps[i].y);
        }
        for (i = 1; i <= m; i++)
        {
            scanf("%d %d", &pt[i].start, &pt[i].end);
            if (pt[i].start != pt[i].end)
            {
                pt[i].dist = getDist(ps[pt[i].start], ps[pt[i].end]);
            }
            else
            {
                pt[i].dist = INF;
            }
        }
        Directed_MST(1, n, m);
    }
    return 0;
}