您的位置:首页 > 其它

uva11183 Teen Girl Squad(最小树形图朱刘算法)

2016-01-28 23:04 495 查看
思路:朱刘算法模板题...

#include <cstdio>
#include <cstring>

const int MAXNODE = 1005;
const int MAXEDGE = 40005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
int u, v;
Type dist;
Edge() {}
Edge(int u, int v, Type dist) {
this->u = u;
this->v = v;
this->dist = dist;
}
};

struct Directed_MST {
int n, m;
Edge edges[MAXEDGE];
int vis[MAXNODE];
int pre[MAXNODE];
int id[MAXNODE];
Type in[MAXNODE];

void init(int n) {
this->n = n;
m = 0;
}

void add_Edge(int u, int v, Type dist) {
edges[m++] = Edge(u, v, dist);
}

void add_Edge(Edge e) {
edges[m++] = e;
}

Type dir_mst(int root) {
Type ans = 0;
while (true) {
for (int i = 0; i < n; i++) in[i] = INF;
for (int i = 0; i < m; i++) {
int u = edges[i].u;
int v = edges[i].v;
if (edges[i].dist < in[v] && u != v) {
in[v] = edges[i].dist;
pre[v] = u;
}
}

for (int i = 0; i < n; i++) {
if (i == root) continue;
if (in[i] == INF) return -1;
}

int cnt = 0;
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(vis));
in[root] = 0;
for (int i = 0; i < n; i++) {
ans += in[i];
int v = i;
while (vis[v] != i && id[v] == -1 && v != root) {
vis[v] = i;
v = pre[v];
}
if (v != root && id[v] == -1) {
for (int u = pre[v]; u != v; u = pre[u])
id[u] = cnt;
id[v] = cnt++;
}
}
if (cnt == 0) break;
for (int i = 0; i < n; i++)
if (id[i] == -1) id[i] = cnt++;
for (int i = 0; i < m; i++) {
int v = edges[i].v;
edges[i].u = id[edges[i].u];
edges[i].v = id[edges[i].v];
if (edges[i].u != edges[i].v)
edges[i].dist -= in[v];
}
n = cnt;
root = id[root];
}
return ans;
}
} gao;

int t, n, m;

int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
gao.init(n);
int u, v, d;
while (m--) {
scanf("%d%d%d", &u, &v, &d);
gao.add_Edge(u, v, d);
}
int tmp = gao.dir_mst(0);
printf("Case #%d: ", ++cas);
if (tmp == -1) printf("Possums!\n");
else printf("%d\n", tmp);
}
return 0;
}


题目

– 3 spring rolls please.– MSG’D!!– Oh! My stomach lining!Strong BadYou are part of a group of n teenage girls armed with cellphones. You have some news you wantto tell everyone in the group. The problem is that no two of you
are in the same room, and you mustcommunicate using only cellphones. What’s worse is that due to excessive usage, your parents haverefused to pay your cellphone bills, so you must distribute the news by calling each other in the cheapestpossible way. You will
call several of your friends, they will call some of their friends, and so on untileveryone in the group hears the news.Each of you is using a different phone service provider, and you know the price of girl A callinggirl B for all possible A and B. Not all
of your friends like each other, and some of them will nevercall people they don’t like. Your job is to find the cheapest possible sequence of calls so that the newsspreads 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 startswith two lines containing n (0 ≤ n ≤ 1000) and m (0 ≤ m ≤ 40, 000). Girls are numbered from 0 ton-1, and you are girl 0. The
next m lines will each contain 3 integers, u, v and w, meaning that a callfrom 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 methodof distributing the news. If there is no solution, print ‘Possums!’ instead.

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

Sample Output

Case #1: 10

Case #2: Possums!

Case #3: 40

Case #4: 130
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: