[POJ][朱算法] 3164 - Command Network@Morris' Blog|PChome Online 人新台
2013-12-01 11:47:04| 人1,758| 回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;
&nbs;   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,758) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: 其他目 |
此分下一篇:[攻防][作1] shell code
此分上一篇:[高等演算法][作三]

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86