24h購物| | PChome| 登入
2013-12-01 11:45:10| 人氣2,165| 回應0 | 上一篇 | 下一篇

[UVA][朱劉算法] 11183 - Teen Girl Squad

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

Problem I
Teen Girl Squad
Input: Standard Input

Output: Standard Output

 

-- 3 spring rolls please.
-- MSG'D!!
-- Oh! My stomach lining!

Strong Bad

You are part of a group of n teenage girls armed with cellphones. You have some news you want to tell everyone in the group. The problem is that no two of you are in the same room, and you must communicate using only cellphones. What's worse is that due to excessive usage, your parents have refused to pay your cellphone bills, so you must distribute the news by calling each other in the cheapest possible way. You will call several of your friends, they will call some of their friends, and so on until everyone in the group hears the news.

Each of you is using a different phone service provider, and you know the price of girl A calling girl B for all possible A and B. Not all of your friends like each other, and some of them will never call people they don't like. Your job is to find the cheapest possible sequence of calls so that the news spreads from you to all n-1 other members of the group.

Input

The first line of input gives the number of cases, N (N<150). N test cases follow. Each one starts with two lines containing n (0<=n<=1000) and m (0 <= m <= 40,000). Girls are numbered from 0 to n-1, and you are girl 0. The next m lines will each contain 3 integers, u, v and w, meaning that a call from girl u to girl v costs w cents (0 <= w <= 1000). No other calls are possible because of grudges, rivalries and because they are, like, lame. The input file size is around 1200 KB.

 

Output

For each test case, output one line containing "Case #x:" followed by the cost of the cheapest method of distributing the news. If there is no solution, print "Possums!" instead.

 

Sample Input                                  Output for Sample Input

4
2
1
0 1 10
2
1
1 0 10
4
4
0 1 10
0 2 10
1 3 20
2 3 30
4
4
0 1 10
1 2 20
2 0 30
2 3 100
Case #1: 10
Case #2: Possums!
Case #3: 40
Case #4: 130
 

Problem setter: Igor Naverniouk

由於老尼克寫題,現在又來重新審視這個演算法問題。
給定一個根,找到有向最小生成樹。
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;
    int v;// x->y, v
    int next;
} edge[200005];
int e, g[1005], vg[1005];// direct graph record
void addEdge(int x, int y, int 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[1005];
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;
    int v;
    memset(visited, 0, sizeof(visited));
    dfs(root);
    for(i = 0; i < n; i++)
        if(visited[i] == 0)
            return -1;
    // solvable
    int prev[1005], dele[1005];
    int ret = 0;
    int vprev[1005];
    int in[1005], out[1005];
    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];
            }
            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 testcase;
    int cases = 0;
    scanf("%d", &testcase);
    while(testcase--) {
        int n, m;
        int i, j, k, u, v, w;
        scanf("%d %d", &n, &m);
        e = 0;
        memset(g, -1, sizeof(g));
        memset(vg, -1, sizeof(vg));
        for(i = 0; i < m; i++) {
            scanf("%d %d %d", &u, &v, &w);
            addEdge(u, v, w);
        }
        int ret = mst(0, n);
        printf("Case #%d: ", ++cases);
        if(ret < 0)
            puts("Possums!");
        else
            printf("%d\n", ret);
    }
    return 0;
}

台長: Morris
人氣(2,165) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 10416 - Folding My T-Shirt
此分類上一篇:[UVA][數學][繁瑣] 10367 - Equations

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