uva 11183 - Teen Girl Squad (最小有向生成树)
来源:互联网 发布:第四类接触知乎 编辑:程序博客网 时间:2024/06/06 12:18
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
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
最小树形图的裸题。
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int INF = 1000000000;const int maxn = 1000 + 10;struct MDST{ int n; int w[maxn][maxn]; int vis[maxn]; int ans; int removed[maxn]; int cid[maxn]; int pre[maxn]; int iw[maxn]; int max_cid; void init(int n) { this -> n = n; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) w[i][j] = INF; } } void AddEdge(int u,int v,int cost) { w[u][v] = min(w[u][v],cost); } int dfs(int s) { vis[s] = 1; int ans = 1; for(int i = 0; i < n; i++) { if(!vis[i] && w[s][i] < INF) ans += dfs(i); } return ans; } bool cycle(int u) { max_cid++; int v = u; while(cid[v] != max_cid) { cid[v] = max_cid; v = pre[v]; } return v == u; } void update(int u) { iw[u] = INF; for(int i = 0; i < n; i++) if(!removed[i] && w[i][u] < iw[u]) { iw[u] = w[i][u]; pre[u] = i; } } bool solve(int s) { memset(vis,0,sizeof(vis)); if(dfs(s) != n) return false; memset(removed,0,sizeof(removed)); memset(cid,0,sizeof(cid)); for(int u = 0; u < n; u++) update(u); pre[s] = s; iw[s] = 0; ans = max_cid = 0; for(;;) { bool have_cycle = false; for(int u = 0; u < n; u++) if(u != s && !removed[u] && cycle(u)) { have_cycle = true; int v = u; do { if(v != u) removed[v] = 1; ans += iw[v]; for(int i = 0; i < n; i++) if(cid[i] != cid[u] && !removed[i]) { if(w[i][v] < INF) w[i][u] = min(w[i][u],w[i][v]-iw[v]); w[u][i] = min(w[u][i],w[v][i]); if(pre[i] == v) pre[i] = u; } v= pre[v]; } while(v != u); update(u); break; } if(!have_cycle) break; } for(int i = 0; i < n; i++) if(!removed[i]) ans += iw[i]; return true; }};MDST solver;int main(){ int t,n,m,u,v,cost,kase = 0; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); solver.init(n); for(int i = 0;i < m;i++){ scanf("%d%d%d",&u,&v,&cost); if(u != v){ solver.AddEdge(u,v,cost); } } printf("Case #%d: ",++kase); if(!solver.solve(0)) printf("Possums!\n"); else printf("%d\n",solver.ans); } return 0;}
- uva 11183 - Teen Girl Squad(有向生成树)
- uva 11183 - Teen Girl Squad (最小有向生成树)
- UVA 11183 - Teen Girl Squad(最小树形图)
- UVA 11183 Teen Girl Squad (最小树形图)
- Uva 11183 Teen Girl Squad(最小树形图 模板)
- uva 11183 Teen Girl Squad (最小树形图)
- UVA-11183-Teen Girl Squad(最小树形图模板)
- UVA 11183 Teen Girl Squad(最小树形图)
- Uva-11183-Teen Girl Squad
- 最小树形图 UVA 11183 Teen Girl Squad
- 【UVa】11183 Teen Girl Squad 最小树形图
- UVA 11183 Teen Girl Squad 最小树形图
- UVA - 11183 Teen Girl Squad(最小树形图)
- UVA 11183 Teen Girl Squad(最小树形图裸题)
- UVA 11183 Teen Girl Squad 最小树形图裸题
- UVA 11183 Teen Girl Squad (最小树形图,3级)
- POJ 3164Command Network && UVA 11183 Teen Girl Squad 最小树形图
- uva11183 Teen Girl Squad(最小树形图朱刘算法)
- 给scott用户解锁
- 发部一篇水文
- 64位ubuntu下装32位libstdc++.so.6
- oracle实验和代码
- I/O File类创建文件夹
- uva 11183 - Teen Girl Squad (最小有向生成树)
- MyEclipse 中配置jconsole监控tomcat内存
- 完全卸载oracle 注册表及文件
- weka 初探
- Oracle 表之间的连接分为三种:
- oracle+110个常用函数
- 很久没写代码了
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connecti...
- 解析STM32启动过程