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. php Aes 128位算法

    <?php class Mcrypt { private static $key = "fsdjfojojodjiovjojgfosdjfiojio"; private st ...

  2. Object源码分析(一)

    刚注册博客,准备学习一下java源码,当然首先从Object看起. 介绍一下Object: Object是所有类层次结构的根,所有的类都将Object作为超类.所有的对象,包括数组,都实现了Objec ...

  3. java中字符串比较==和equals

    1 总体来说java中字符串的比较是==比较引用,equals 比较值的做法.(equals 对于其他引用类型比较的是地址,这是因为object的equals方法比较的是引用),但是不同的声明方法字符 ...

  4. QlikSense系列(4)——QlikSense管理

    QlikSense管理主要通过QMC界面,在安装成功后,首先需要导入用户,QlikSense本身不能创建和验证用户,只能借助第三方系统, 笔者只使用过Windows账户和AD域用户: 1.Window ...

  5. 第4章 部署模式 Deployment Plan(部署规划)

    已开发了基于组件的应用程序,该应用程序在逻辑上构造为多层结构,如 Three-Layered Services Application. 中所述.您希望将它分布到一组在物理上为多级结构的服务器上,如 ...

  6. jQuery操作样式知识总结

    css操作 功能:设置或者修改样式,操作的是style属性. 设置单个样式 //name:需要设置的样式名称 //value:对应的样式值 css(name, value); //使用案例 $(&qu ...

  7. [OpenWrt]安装mjpg-streamer

    安装mjpg-streamer 远程监控基本上是wifi小车的一个必备功能了.摄像头我用的是奥尼百脑通 D881,这个要100左右. 确认安装了以下软件: kmod-usb2 kmod-video-u ...

  8. Oracle PL/SQL开发基础(第三十四弹:RAISE_APPLICATION_ERROR)

    RAISE_APPLICATION_ERROR在子程序内部使用时,能从存储子程序中抛出自定义的错误消息.这样就能将错误报告给应用程序而避免范围未捕获异常. 语法如下: RAISE_APPLICATIO ...

  9. java学习笔记3——异或

    异或原理: 转换两个字符或数为2进制的ASCII码,再按位异或,即 0001 0001 ---> 0000 0000 0000 ---> 0000 0001 0000 ---> 00 ...

  10. Matrix(坑)

    https://github.com/florent37/Android-3D-Layout