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. luogu1908 逆序对 树状数组

    题目大意:对于给定的一段正整数序列,逆序对就是序列中ai>aj且i<j的有序对.求一段序列的逆序对数. 对于一个数组T,其一个点的值为值与该点下标相等的A序列中点的个数.对T维护一个树状数 ...

  2. jsp页面自动换行

    其实只要在表格控制中添加一句 <div style="word-break:break-all">就搞定了.</div>其中可能对英文换行可能会分开一个单词 ...

  3. log4net写日志的时间附带时区信息

    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss.fffzzz} [%thread] %-5level %logger - %me ...

  4. 【NOIP 2009】 Hankson的趣味题

    [题目链接] https://www.luogu.org/problemnew/show/P1072 [算法] x是b1的约数 筛出b1的约数,判断是否符合条件即可 [代码] #include< ...

  5. RT-Thread 设备驱动SPI浅析及使用

    OS版本:RT-Thread 4.0.0 测试BSP:STM32F407 SPI简介 SPI总线框架其实和I2C差不多,可以说都是总线设备+从设备,但SPI设备的通信时序配置并不固定,也就是说控制特定 ...

  6. 利用JavaScript的%读分秒

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. GEF中连接的实现

    在GEF绘图笔想象中复杂许多,需要很多组件的依赖与支持,稍微弄错一个引用,或一个操作调试起来就比较麻烦,下面列一下实现一个连接线功能所需要实现的类及添加的方法 建议大图查看. 相关代码:参考<G ...

  8. 记我安装Caffe的血泪史(1)

    Caffe的安装我主要参考的是这篇博客 http://blog.csdn.net/ubunfans/article/details/47724341 按照他的方法我很快就安好了cuda8(为后面安装了 ...

  9. Spark的协同过滤.Vs.Hadoop MR

    基于物品的协同过滤推荐算法案例在TDW Spark与MapReudce上的实现对比,相比于MapReduce,TDW Spark执行时间减少了66%,计算成本降低了40%. 原文链接:http://w ...

  10. OpenCV_Python教程 系列!

    这个是作者的总结系列!赞一个! 原文链接:http://blog.csdn.net/sunny2038/article/details/9057415 在python中使用OpenCV:http:// ...