Problem I
Teen Girl Squad 
Input: Standard Input Output: Standard Output

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)and m (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.


Problem setter: Igor Naverniouk
 
解题思路:最小树形图 模板题
 
 #include<cstdio>
#include<cstring>
#define SIZE 1002
#define MAXN 40004 using namespace std; const int INF = <<;
int nv, ne, N;
int vis[SIZE], pre[SIZE], num[SIZE];
int inLength[SIZE]; struct Edge{
int u, v, w;
}edge[MAXN]; bool Traverse(int& res)
{
int root = ;
while(true)
{
for(int i=; i<nv; ++i) inLength[i] = INF;
for(int i=; i<ne; ++i)
{
int& u = edge[i].u;
int& v = edge[i].v;
if(edge[i].w < inLength[v] && u != v)
{
inLength[v] = edge[i].w;
pre[v] = u;
}
}
for(int i=; i<nv; ++i)
if(i != root && inLength[i] == INF) return false;
int newnum = ;
inLength[root] = ;
memset(vis, -, sizeof(vis));
memset(num, -, sizeof(num));
for(int i=; i<nv; ++i)
{
res += inLength[i];
int v = i;
while(vis[v] != i && num[v] == - && v != root)
{
vis[v] = i;
v = pre[v];
}
if(vis[v] == i)
{
for(int u=pre[v]; u != v; u = pre[u])
num[u] = newnum;
num[v] = newnum++;
}
}
if(newnum == ) return true;
for(int i=; i<nv; ++i)
if(num[i] == -) num[i] = newnum++;
for(int i=; i<ne; ++i)
{
int u = edge[i].u;
int v = edge[i].v;
edge[i].u = num[u];
edge[i].v = num[v];
if(edge[i].u != edge[i].v)
edge[i].w -= inLength[v];
}
nv = newnum;
root = num[root];
} } int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%d", &N);
for(int t = ; t <= N; ++t)
{
int u, v, w;
scanf("%d%d", &nv, &ne);
for(int i=; i<ne; ++i)
{
scanf("%d%d%d", &u, &v, &w);
edge[i].u = u;
edge[i].v = v;
edge[i].w = u == v ? INF+ : w;
}
printf("Case #%d: ", t);
int res = ;
if(Traverse(res)) printf("%d\n", res);
else printf("Possums!\n");
}
return ;
}
 

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

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

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

  2. UVA:11183:Teen Girl Squad (有向图的最小生成树)

    Teen Girl Squad Description: You are part of a group of n teenage girls armed with cellphones. You h ...

  3. uva 11183 Teen Girl Squad

    题意: 有一个女孩,需要打电话让所有的人知道一个消息,消息可以被每一个知道消息的人传递. 打电话的关系是单向的,每一次电话需要一定的花费. 求出打电话最少的花费或者判断不可能让所有人知道消息. 思路: ...

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

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

  5. UVa11183 - Teen Girl Squad(最小树形图-裸)

    Problem I Teen Girl Squad  Input: Standard Input Output: Standard Output -- 3 spring rolls please. - ...

  6. UVA 11865 Stream My Contest(最小树形图)

    题意:N台机器,M条有向边,总资金C,现要到搭建一个以0号机(服务器)为跟的网路,已知每条网线可以把数据从u传递到v,其带宽为d,花费为c,且d越大,传输速度越快,问能够搭建的传输速度最快的网络d值是 ...

  7. Stream My Contest UVA - 11865(带权最小树形图+二分最小值最大化)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

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

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

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

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

随机推荐

  1. 第十一篇 Material Status设置与测试,制药业案例一则

    详见,http://bbs.erp100.com/thread-273173-1-1.htmlMaterial Status不同于Item Status.Item Status用于统一控制Item的s ...

  2. arranging-coins

    https://leetcode.com/problems/arranging-coins/ public class Solution { public int arrangeCoins(int n ...

  3. tc srm 636 div2 500

    100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...

  4. java线程安全理解

    java线程安全理解 如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的. ...

  5. [转载]java的几种对象(PO,VO,DAO,BO,POJO)解释

    原文地址:java的几种对象(PO,VO,DAO,BO,POJO)解释作者:munandong 一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最 ...

  6. 【笨嘴拙舌WINDOWS】字符类型与字符串

    "我将用C语言作为工具,开始WINDOWS API的使用" windows NT 从底层开始支持unicode. 1.字符类型 WINDOWS的字符类型在WINNT.H和CTYPE ...

  7. C#操作office进行Excel图表创建,保存本地,word获取

    ,新建C#控制台应用程序(Excel创建图表) using System; using System.Collections.Generic; using System.Linq; using Sys ...

  8. Codeforces 475 B Strongly Connected City【DFS】

    题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...

  9. C#随机颜色和随机字母

    //随机获取颜色 public System.Drawing.Color GetRandomColor() { Random RandomNum_First = new Random(Guid.New ...

  10. Ubuntu中文输入法的安装

    Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu的中文系统中自带了中文输入法,通过Ctrl+S ...