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. WebP 文件及其编码解码工具(WebPconv)

    1. webp 文件 与JPEG相同,WebP 是一种有损压缩利用预测编码技术. WebP 是 Google 新推出的影像技术,它可让网页图档有效进行压缩,同时在质量相同的情况下,WebP 格式图像的 ...

  2. 根据日期获取,x岁x月x天

    c#: DateTime startDate = new DateTime(); DateTime endDate = new DateTime(); ; ; ; if (endDate.Month& ...

  3. Hdu-6230 2017CCPC-哈尔滨站 A.Palindrome Manacher 主席树

    题面 题意:给你一个字符串,问你满足s[i]=s[2n-i]=s[2n+i-2]的子串(这子串长度为3n-2)有多少个,原字符串长度<=5e5 题解:对于这种子串,其实要满足2个回文,跑过一次M ...

  4. C Linux read write function extension

    前言 - 赠送 readn / writen Linux 上默认的 read 和 write 函数会被信号软中断. 且 read 和 write 函数中第三个参数 count #include < ...

  5. ps -aux ,ps aux ,ps -ef 的区别

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  6. Selenium之当鼠标悬浮时隐藏的元素才出现

    在自动化过程中,有些导航按钮只有当鼠标悬浮在登录信息上时,它才能出现.这时候如果想要点击导航按钮直接用selenium的webDriver是无法定位的元素的,因为这些元素是隐藏的,只有鼠标悬浮时才出现 ...

  7. 装饰模式(Decorator)C++实现

    装饰模式 层层包装,增强功能.这就是装饰模式的要旨!装饰器模式就是基于对象组合的方式,可以很灵活的给对象添加所需要的功能.它把需要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象. 意图: 动 ...

  8. Nginx配置Q&A

    隐藏响应头 How can remove Nginx from http response header? - Stack Overflow more_set_headers 'Server: my- ...

  9. outlook导入配置文件

    公司入.离职人员越来越多,所以产生了一个自动化配置邮件的想法 查看了一下资料,outlook有导入配置文件的方法可用. 利用otc工具,打开office2010的安装文件夹,执行setup.exe / ...

  10. Caffe: Caffe的Python接口

    官方参考:http://caffe.berkeleyvision.org/installation.html 官方介绍是这样的: Python The main requirements are nu ...