24h購物| | PChome| 登入
2014-01-24 10:31:23| 人氣619| 回應0 | 上一篇 | 下一篇

[UVA][SSSP] 10850 - The Gossipy Gossipers Gossip Gossips

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

 The Gossipy Gossipers Gossip Gossips 

"Gossipy gossiper, what did you get of so much gossiping?
You set your soul into hell. Behold your great achievement!"

M.A. Hevia. Mermuradora.

The Problem

Gossiping mechanism is quite simple, but effective. Everything starts with a master gossiper (most probably a she), who hears, witnesses, or makes up some extraordinary news. Whenever she meets another person, she tells him the news. As soon as these people meet others, they tell them the news. Soon, everybody is informed of the news, and the process finishes.

Gossipers meet each other daily at the same hours. We have information on when every pair of gossipers meet. For simplicity, days are divided into 100 instants. The first day goes from 0 to 99, the second from 100 to 199, and so on.

Suppose the gossiping process starts at time 0. When will it finish? That is, when will all the people be informed of the news?

The Input

The first line of the input contains an integer N, indicating the number of test cases.

For each test case, the first line contains two integers M and K. M indicates the number of people in this case (numbered from 1 to M). The master gossiper is always number 1. K indicates the number of pairs of people who meet.

Next, we have 2K lines (two lines for each pair of people who meet). The first line of each pair contains three integers, G1, G2 and V. This line means that gossipers G1 and G2 meet V times a day. The second line contains V numbers from 1 to 99, that indicate the instants when G1 and G2 meet daily.

For example, the pair:

3 7 2
28 88
means: person 3 and person 7 meet twice a day, at instants 28 and 88 everyday (that is, at 28, 88, 128, 188, 228, 288, ...).

You can assume that M ≤ 20, KM*M, and V ≤ 12 .

The Output

For each test case, the output should consist of a single integer F in one line, indicating the instant when the process finishes. If the process does not finish (for example, there is some isolated person who will never be informed), the result should be -1.

Sample Input

4
4 3
1 2 1
60
2 3 1
40
4 3 1
30
3 2
1 2 1
42
2 3 1
42
5 5
1 3 2
60 70
1 4 3
20 22 24
4 5 1
10
3 5 2
12 80
3 2 2
55 78
8 0

Sample Output

230
42
80
-1
*Warning: "gossips" have nothing to do with "ghost ships".


題目描述:

八卦傳遞,每一天可以劃分成 100 個時槽 [0, 99],
問從編號 1 (從時刻 0 開始)將八卦散布於全部人都知道的最少時間為何。

給予 x y 之間會在哪些時刻交換訊息,逼不得已的情況下必須到下一天進行。

題目解法:


SSSP,單源最短路徑。

#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
struct edge {
    int to, time;
    edge(int a, int b):
        to(a), time(b) {}
};
vector<edge> g[25];
void spfa(int n) {
    int dp[25][105], inq[25][105];
    int i, j, k;
    int x, t, y, w;
    for(i = 1; i <= n; i++)
        for(j = 0; j < 100; j++)
            dp[i][j] = 0xfffffff, inq[i][j] = 0;
    dp[1][0] = 0;
    queue<int> X, TIME;
    X.push(1), TIME.push(0);
    while(!X.empty()) {
        x = X.front(), X.pop();
        t = TIME.front(), TIME.pop();
        inq[x][t] = 0;
        for(i = 0; i < g[x].size(); i++) {
            y = g[x][i].to, w = g[x][i].time;
            int gap;
            if(w < t)    gap = 100 - t + w;
            else        gap = w - t;
            if(dp[y][w] > dp[x][t] + gap) {
                dp[y][w] = dp[x][t] + gap;
                if(inq[y][w] == 0) {
                    inq[y][w] = 1;
                    X.push(y), TIME.push(w);
                }
            }
        }
    }
    int ret = 0;
    for(i = 1; i <= n; i++) {
        int temp = 0xfffffff;
        for(j = 0; j < 100; j++)
            temp = min(temp, dp[i][j]);
        ret = max(ret, temp);
        if(temp == 0xfffffff) {
            ret = -1;
            break;
        }
    }
    printf("%d\n", ret);
}
int main() {
    int testcase;
    int n, m, x, y, t, time;
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d", &n, &m);
        for(i = 1; i <= n; i++)
            g[i].clear();
        for(i = 0; i < m; i++) {
            scanf("%d %d %d", &x, &y, &t);
            while(t--) {
                scanf("%d", &time);
                g[x].push_back(edge(y, time));
                g[y].push_back(edge(x, time));
            }
        }
        spfa(n);
    }
    return 0;
}

台長: Morris
人氣(619) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][樹形dp] 10859 - Placing Lampposts
此分類上一篇:[UVA][Legendre Symbol] 10831 - Gerg's Cake

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