UVA:11183:Teen Girl Squad (有向图的最小生成树)
Teen Girl Squad
Description:
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 (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, u, v 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:
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
Sample Output:
Case #1: 10
Case #2: Possums!
Case #3: 40
Case #4: 130
题解:
有向图的最小生成树模板题,用朱刘算法即可解决。
代码如下:
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,t;
const int N = ,M = ;
struct Edge{
int u,v,w;
}e[M];
int pre[N]; //记录前驱.
int id[N],vis[N],in[N];
int dirMst(int root){
int ans=;
while(){
memset(in,INF,sizeof(in));
memset(id,-,sizeof(id));
memset(vis,-,sizeof(vis));
for(int i=;i<=m;i++){
int u=e[i].u,v=e[i].v,w=e[i].w;
if(w<in[v] && v!=u){
pre[v]=u;
in[v]=w;
}
} //求最小入边集
in[root]=;
pre[root]=root;
for(int i=;i<n;i++){
if(in[i]==INF) return -;
ans+=in[i];
}
int idx = ; //新标号
for(int i=;i<n;i++){
if(vis[i] == - ){
int u = i;
while(vis[u] == -){
vis[u] = i;
u = pre[u];
}
if(vis[u]!=i || u==root) continue; //判断是否形成环
for(int v=pre[u];v!=u;v=pre[v] )
id[v]=idx;
id[u] = idx++;
}
}
if(idx==) break;
for(int i=;i<n;i++){
if(id[i]==-) id[i]=idx++;
}
for(int i=;i<=m;i++){
e[i].w-=in[e[i].v];
e[i].u=id[e[i].u];
e[i].v=id[e[i].v];
}
n = idx;
root = id[root];//给根新的标号
}
return ans;
} int main(){
cin>>t;
int cnt = ;
while(t--){
cnt++;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
printf("Case #%d: ",cnt);
int res=dirMst();
if(res==-) puts("Possums!");
else cout<<res<<endl;
}
}
UVA:11183:Teen Girl Squad (有向图的最小生成树)的更多相关文章
- Uva 11183 - Teen Girl Squad (最小树形图)
Problem ITeen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n ...
- UVA 11183 Teen Girl Squad 最小树形图
最小树形图模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <c ...
- uva 11183 Teen Girl Squad
题意: 有一个女孩,需要打电话让所有的人知道一个消息,消息可以被每一个知道消息的人传递. 打电话的关系是单向的,每一次电话需要一定的花费. 求出打电话最少的花费或者判断不可能让所有人知道消息. 思路: ...
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
Teen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n teenage ...
- HDU4009:Transfer water(有向图的最小生成树)
Transfer water Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)To ...
- UVA11183 Teen Girl Squad —— 最小树形图
题目链接:https://vjudge.net/problem/UVA-11183 You are part of a group of n teenage girls armed with cell ...
- UVa11183 - Teen Girl Squad(最小树形图-裸)
Problem I Teen Girl Squad Input: Standard Input Output: Standard Output -- 3 spring rolls please. - ...
- 【UVA 11183】 Teen Girl Squad (定根MDST)
[题意] 输入三元组(X,Y,C),有向图,定根0,输出MDST. InputThe first line of input gives the number of cases, N (N < ...
- Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)
解析: 裸的有向图最小生成树 代码 #include<cstdio> #include<cstring> #include<string> #include< ...
随机推荐
- es6笔记4^_^function
一.function默认参数 现在可以在定义函数的时候指定参数的默认值了,而不用像以前那样通过逻辑或操作符来达到目的了. es5 function sayHello(name){ //传统的指定默认参 ...
- 使用getid3获取音频文件信息
今天有个需求,在上传音频文件时候自动获取音频的秒数,和大家分享一下. 首先把getid3的包下载下来 链接:https://pan.baidu.com/s/1Qmdj-I4boz9Sm9GFsON0D ...
- CSS让内部元素以相反的顺序显示
代码如下: <div id="main" style=" flex-direction: row-reverse;-webkit-flex-direction: r ...
- Django学习总结-之-URLS反向解析
2018-09-15 09:58:49 在CSDN博客审核效率提高之前, 又要在此处向各位唠叨了~ URL 与 URI URL : 统一资源定位符 相当于绝对路径 URI : 统一资源标志符 相当于 ...
- lintcode142 O(1)时间检测2的幂次
O(1)时间检测2的幂次 用 O(1) 时间检测整数 n 是否是 2 的幂次. 您在真实的面试中是否遇到过这个题? Yes 样例 n=4,返回 true; n=5,返回 false. 二进制的n中只有 ...
- 本地矩阵(Local Matrix)
本地矩阵具有整型的行.列索引值和双精度浮点型的元素值,它存储在单机上.MLlib支持稠密矩阵DenseMatrix和稀疏矩阵Sparse Matrix两种本地矩阵,稠密矩阵将所有元素的值存储在一个列优 ...
- Period :KMP
I - Period Problem Description For each prefix of a given string S with N characters (each character ...
- [C++] OOP - Access Control and Class Scope
Access Control And Inheritance Protected Member Like private, protected members are unaccessible to ...
- JS中Document节点总结
document对象是documentHTML的一个实例,也是window对象的一个属性,因此可以将document对象作为一个全局对象来访问. Document节点的子节点可以是DocumentTy ...
- NFC学习总结二
移动支付这事情热了总归还是会回归理性,就如同之前的10几年间的几次轮回一样.字面上看,移动支付比支付大也不大可能,有相同,有扩展,有交集有不通才是. NFC这事情也是说了快十年了,真心希望它能回归到其 ...