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 <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
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<edge>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;
}

UVa11183 - Teen Girl Squad(最小树形图-裸)的更多相关文章

  1. UVA11183 Teen Girl Squad —— 最小树形图

    题目链接:https://vjudge.net/problem/UVA-11183 You are part of a group of n teenage girls armed with cell ...

  2. UVA 11183 Teen Girl Squad 最小树形图

    最小树形图模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <c ...

  3. UVa11183 Teen Girl Squad, 最小树形图,朱刘算法

    Teen Girl Squad  Input: Standard Input Output: Standard Output You are part of a group of n teenage ...

  4. UVA-11183 Teen Girl Squad (最小树形图、朱刘算法模板)

    题目大意:给一张无向图,求出最小树形图. 题目分析:套朱-刘算法模板就行了... 代码如下: # include<iostream> # include<cstdio> # i ...

  5. Codeforces 240E. Road Repairs 最小树形图+输出路径

    最小树形图裸题,只是须要记录路径 E. Road Repairs time limit per test 2 seconds memory limit per test 256 megabytes i ...

  6. POJ3436 Command Network [最小树形图]

    POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...

  7. Uva 11183 - Teen Girl Squad (最小树形图)

    Problem ITeen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n  ...

  8. 最小树形图模板 UVA11183

    题意:给定n个节点m条边的有向带权图,求以0为根节点的最小树形图权值大小 用这个代码的时候要注意,这里的数据是从0开始的,边也是从0开始算, 所以在打主代码的时候,如果是从1开始,那么算法里面的从0开 ...

  9. kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数

    第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...

随机推荐

  1. The name ‘InitialzeComponent’ does not exist in the current context

    在Visual Studio中创建Windows Store项目,在MainPage.xaml.cs中出现错误: The name 'InitialzeComponent' does not exis ...

  2. T对象序列化后T对象中属性字段不见了?

    序列化:JsonConvert.SerializeObject(T) 直接在类的上面添加[Table("表名")] 在类上添加属性[DataContract] 在属性上添加属性[D ...

  3. 第6章 服务模式 在 .NET 中实现 Service Gateway(服务网关)

    上下文 您正在设计企业应用程序,该程序需要使用由其他应用程序提供的服务.该服务定义了一个合约,所有服务使用者要访问该服务都必须遵守该合约.该合约定义了与此服务通信所需的技术.通信协议和消息定义等内容. ...

  4. X264编码实现

    H264 H264的官方测试源码,由德国hhi研究所负责开发.特点:实现了264所有的特性,由于是官方的测试源码,所以学术研究的算法都是在JM基础上实现并和JM进行比较.但其程序结构冗长,只考虑引入各 ...

  5. GCC编译步骤

    gcc -E t1.c -o t1.i 预处理gcc -S t1.i -o t1.s 转成汇编语言gcc -c t1.s -o t1.o 转成机器码gcc t1.o -o t1.exe 链接 直接使用 ...

  6. okhttp3 ExceptionInInitializerError 异常处理

    okhttp3 在Android4.4上出现下面异常 java.lang.ExceptionInInitializerError at okhttp3.OkHttpClient.newSslSocke ...

  7. in 与 exist , not in 与 not exist

    in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的两个 ...

  8. Kafka学习笔记(7)----Kafka使用Cosumer接收消息

    1. 什么是KafkaConsumer? 应用程序使用KafkaConsul'le 「向Kafka 订阅主题,并从订阅的主题上接收消息.Kafka的消息读取不同于从其他消息系统读取数据,它涉及了一些独 ...

  9. CorelDRAW购物节福利开奖 | “剁手节”前的攻略来了,请查收!

    距离“剁手节” 还有不到两周的时间 相信一些线上.线下.微商.电商...都拿出了看家本领进行市场混战 一场没有硝烟的战争开始啦!! …… 当然,我也不例外 你们谁都别拦着,我要打广告了 让你变成了不折 ...

  10. 使用DOS命令查找包含某一字符串的所有文件

      在windows系统下,来查找并修改指定目录下包含某一字符串的所有文件,麻烦又费时.其实在DOS命令中,提供了Findstr命令来查找指定的一个或多个文件文件中包含(或通过参数 /V来控制不包含) ...