24h購物| | PChome| 登入
2013-12-01 11:47:04| 人氣1,467| 回應0 | 上一篇 | 下一篇

[POJ][朱劉算法] 3164 - Command Network

推薦 0 收藏 0 轉貼0 訂閱站台


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

Source


由於老尼克寫題,現在又來重新審視這個演算法問題。
給定一個根,找到有向最小生成樹。
Prim 和 Kruskal 是派不上用場的,但多少有點關係。
首先,基於貪婪的思路,先將根排除在外,
將其他點找到各自的最小權重入邊,明顯地這種方式,
符合需求,每一個非根點一定要有入邊,才能有解。

然而問題現在才來,如果有環怎麼辦?
考慮將這個環縮成一點,將邊指向環的權重稍微修改一番,
仔細思考一下,v 在環上(w 也是),原本選定 wv 最小,
現在找到一個次小的 uv (u 在外面),將這條邊的權重修正
為 uv-wv,假使我們選到這一條邊時,這個最小樹的成本最增加
uv-wv,而實際情況則是將 wv 移除,納入 uv,將環拆開。


#include <stdio.h>
#include <vector>
#include <string.h>
#include <math.h>
using namespace std;
struct Node {
    int x, y;
    double v;// x->y, v
    int next;
} edge[100005];
int e, g[105], vg[105];// direct graph record
void addEdge(int x, int y, double v) {
    if(x == y)    return;
    edge[e].x = x, edge[e].y = y, edge[e].v = v;
    edge[e].next = g[x], g[x] = e++;
    edge[e].x = y, edge[e].y = x, edge[e].v = -127;//this weight not important.
    edge[e].next = vg[y], vg[y] = e++;
}
int visited[105];
void dfs(int x) {
    visited[x] = 1;
    for(int i = g[x]; i != -1; i = edge[i].next) {
        if(!visited[edge[i].y])
            dfs(edge[i].y);
    }
}
double mst(int root, int n) {//node [0, n-1]
#define oo 1000000000
    int i, j, k;
    int u;
    double v;
    memset(visited, 0, sizeof(visited));
    dfs(root);
    for(i = 0; i < n; i++)
        if(visited[i] == 0)
            return -1;
    // solvable
    int prev[105], dele[105];
    double ret = 0;
    double vprev[105];
    memset(dele, 0, sizeof(dele));
    while(true) {
        for(i = 0; i < n; i++)
            prev[i] = i, vprev[i] = oo;
        for(i = 0; i < n; i++) {// O(E)
            if(dele[i])
                continue;
            for(j = g[i]; j != -1; j = edge[j].next) {
                u = edge[j].y, v = edge[j].v;
                if(dele[u])
                    continue;
                if(vprev[u] > v)
                    vprev[u] = v, prev[u] = i;
            }
        }
        for(i = 0; i < n; i++) {
            if(dele[i] || i == root)
                continue;
            j = i; // find cycle
            memset(visited, 0, sizeof(visited));
            visited[root] = 1;
            do visited[j] = 1, j = prev[j];
            while(!visited[j]);
            if(j == root)    continue;
            i = j;
            ret += vprev[i];
            for(j = prev[i]; j != i; j = prev[j]) {
                ret += vprev[j];
                dele[j] = 1;
            }
            // modify edge weight.
            //printf("modify edge weight\n");
            for(j = vg[i]; j != -1; j = edge[j].next) {
                u = edge[j].y;// u->i
                if(dele[u])
                    continue;
                edge[j^1].v -= vprev[i];
            }
            double in[105], out[105];
            for(k = 0; k < n; k++)
                in[k] = oo, out[k] = oo;
            for(j = prev[i]; j != i; j = prev[j]) {
                for(k = vg[j]; k != -1; k = edge[k].next) {
                    u = edge[k].y, v = edge[k^1].v;// u->cycle
                    if(dele[u])
                        continue;
                    v -= vprev[j];
                    in[u] = min(in[u], v);
                }
                for(k = g[j]; k != -1; k = edge[k].next) {
                    u = edge[k].y, v = edge[k].v;// cycle->u
                    if(dele[u])
                        continue;
                    out[u] = min(out[u], v);
                }
            }
            for(k = 0; k < n; k++) {
                if(k == i)
                    continue;
                if(in[k] != oo)
                    addEdge(k, i, in[k]);
                if(out[k] != oo)
                    addEdge(i, k, out[k]);
            }
            break;// repeat for a new graph.
        }
        if(i == n) {// there not exist cycle.
            for(i = 0; i < n; i++) {
                if(dele[i] || i == root)
                    continue;
                //printf("%lf\n", vprev[i]);
                ret += vprev[i];
            }
            break;
        }
    }
    return ret;
}
int main() {
    int n, m;
    int i, j, k, u, v;
    int x[105], y[105];
    while(scanf("%d %d", &n, &m) == 2) {
        e = 0;
        memset(g, -1, sizeof(g));
        memset(vg, -1, sizeof(vg));
        for(i = 0; i < n; i++)
            scanf("%d %d", &x[i], &y[i]);
        for(i = 0; i < m; i++) {
            scanf("%d %d", &u, &v);
            u--, v--;
            addEdge(u, v, hypot(x[u]-x[v], y[u]-y[v]));
        //    printf("%d %d - %lf\n", u, v, hypot(x[u]-x[v], y[u]-y[v]));
        }
        double ret = mst(0, n);
        if(ret < 0)
            puts("poor snoopy");
        else
            printf("%.2f\n", ret);
    }
    return 0;
}

台長: Morris
人氣(1,467) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: 其他題目 |
此分類下一篇:[電腦攻防][作業1] shell code
此分類上一篇:[高等演算法][作業三] 討論

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文