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 (有向图的最小生成树)的更多相关文章

  1. Uva 11183 - Teen Girl Squad (最小树形图)

    Problem ITeen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n  ...

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

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

  3. uva 11183 Teen Girl Squad

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

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

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

  5. HDU4009:Transfer water(有向图的最小生成树)

    Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)To ...

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

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

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

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

  8. 【UVA 11183】 Teen Girl Squad (定根MDST)

    [题意] 输入三元组(X,Y,C),有向图,定根0,输出MDST. InputThe first line of input gives the number of cases, N (N < ...

  9. Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)

    解析: 裸的有向图最小生成树 代码 #include<cstdio> #include<cstring> #include<string> #include< ...

随机推荐

  1. 【转】一款已上市MMO手游地图同步方案总结

    转自游戏开发主席 1. 客户端地图格子的相关知识 在2.5D的MMO游戏里,角色是通过3D的方式渲染,2D的地图是通过2D的方式显示,所以在客户端一般会有三个坐标系: a) 3D坐标系:所有需要3D渲 ...

  2. CSP201403-2:窗口

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  3. 标注点(Labeled Point)

    标注点LabeledPoint是一种带有标签(Label/Response)的本地向量,它可以是稠密或者是稀疏的.在MLlib中,标注点在监督学习算法中被使用.由于标签是用双精度浮点型来存储的,故标注 ...

  4. Android开发-API指南-<activity>

    <activity> 英文原文:http://developer.android.com/guide/topics/manifest/activity-element.html 采集(更新 ...

  5. python邮件服务-yagmail

      下载安装 yagmail import yagmail #链接邮箱服务器 #此处的password是授权码 yag= yagmail.SMTP( user="843092012@qq.c ...

  6. 【转载】2015Android 面试题 01

    1.如何避免ANR? 答:ANR:Application Not Responding,五秒在Android中,活动管理器和窗口管理器这两个系统服务负责监视应用程序的响应. 当出现下列情况时,Andr ...

  7. HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

    Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...

  8. 上层应用与wpa_supplicant,wpa_supplicant与kernel 相关socket创建交互分析

    单独拿出来,分析以下上层应用与wpa_supplicant   wpa_supplicant与kernel 的socket交互. 关联上层应用与wpa_supplicant的socket的创建.连接流 ...

  9. 软件功能-东北师大站-第三次作业(PSP)

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 本周PSP饼状图

  10. 算法与数据结构实验题 6.3 search

    ★实验任务 可怜的 Bibi 刚刚回到家,就发现自己的手机丢了,现在他决定回头去搜索 自己的手机. 现在我们假设 Bibi 的家位于一棵二叉树的根部.在 Bibi 的心中,每个节点 都有一个权值 x, ...