uva11183 Teen Girl Squad(最小树形图朱刘算法)
2016-01-28 23:04
495 查看
思路:朱刘算法模板题...
题目
– 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
#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
相关文章推荐
- NOIP 2015 D1 T2 信息传递
- 【工具】【电子设计】超屌的 fritzing 新建元件
- Java常见错误汇总
- dictHsCode is not mapped
- Leetcode 298 Binary Tree Longest Consecutive Sequence
- Oracle与Tomcat端口冲突,报错:Protocol handler initialization failed;Socket bind failed
- hdu(1171)——Big Event in HDU
- struts2乱码
- input文本框只能输入数字
- oracle VPD
- javascript 中的this call apply
- android-继承BaseAdapter自定义适配器,getView执行多次的解决方法
- 99%学生都不知道的Paper写作神器,Google从此是路人
- Android中将方形图片截取成圆形的两种实现方式
- 编写可读代码的艺术chapter 10-13
- input文本框只能输入数字
- 颜色数组
- 再谈QVector与QByteArray——Qt的写时复制(copy on write)技术
- leapMotion概述(API Overview)
- jQuery+HTML5弹出创意搜索框层