博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVa11183 - Teen Girl Squad(最小树形图-裸)
阅读量:7072 次
发布时间:2019-06-28

本文共 3328 字,大约阅读时间需要 11 分钟。

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<150). N test cases follow. Each one starts with two lines containing n (0<=n<=1000) andm (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, uv 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
 
#include 
#include
#include
#include
#include
#include
#include
using namespace std;const int maxn = 1000+10;const int inf = 1<<25;struct edge{ int u,v,w; edge(int u,int v,int w):u(u),v(v),w(w){}};vector
e;int n,m;int pre[maxn],inv[maxn],to[maxn],ID[maxn];int ZhuLiu(int rt){ int ret = 0; while(true){ for(int i = 0; i < n; i++){ inv[i] = inf; ID[i] = -1; to[i] = -1; } for(int i = 0; i < m; i++){ int u = e[i].u,v = e[i].v,w = e[i].w; if(inv[v] > w && u != v){ inv[v] = w; pre[v] = u; } } inv[rt] = 0; for(int i = 0; i < n; i++){ if(inv[i] == inf) return -1; } int cnt = 0; for(int i = 0; i < n; i++){ ret += inv[i]; int v = i; while(to[v] != i && ID[v]==-1 && v != rt){ to[v] = i; v = pre[v]; } if(ID[v]==-1 && v != rt){ 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 u = e[i].u,v = e[i].v, w = e[i].w; e[i].w -= inv[v]; e[i].u = ID[u]; e[i].v = ID[v]; } rt = ID[rt]; n = cnt; } return ret;}int main(){ int ncase,T=1; cin >> ncase; while(ncase--){ e.clear(); scanf("%d%d",&n,&m); for(int i = 0; i < m; i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); e.push_back(edge(u,v,w)); } int ans = ZhuLiu(0); if(ans==-1){ printf("Case #%d: Possums!\n",T++); }else{ printf("Case #%d: %d\n",T++,ans); } } return 0;}

转载地址:http://nzkml.baihongyu.com/

你可能感兴趣的文章
使用WCF Test Client(WcfTestClient.exe) 来测试WCF
查看>>
综合应用WPF/WCF/WF/LINQ之三十:代码生成器之DBMLToDAL
查看>>
.NET开源项目介绍及资源推荐:数据持久层
查看>>
RAC同单实例物理备库的switchover
查看>>
MultiRow发现之旅(三)- 模板管理器和Table
查看>>
Exchange 2010与Exchange Online混合部署PART 4:混合部署
查看>>
panama项目中字符编码问题解决
查看>>
向C#的String类添加按字节截取字符串的扩展方法
查看>>
Python中元组、列表、字典的遍历和相互转化
查看>>
SmoothWall的小企业应用
查看>>
FOSCommentBundle功能包:设置Doctrine ORM映射(投票)
查看>>
Enterprise Library Step By Step系列(十五):配置应用程序块——设计篇
查看>>
根据要素选择集,创建新图层
查看>>
Android Framework 记录之二
查看>>
Hadoop集群(第10期)_MySQL关系数据库
查看>>
AE与AO的区别
查看>>
隐马尔可夫模型(四)——隐马尔可夫模型的评估问题(后向算法)
查看>>
Javascript语法基础
查看>>
设计模式(十五):Iterator迭代器模式 -- 行为型模式
查看>>
A.2.3-猜数字游戏
查看>>