UVA11183 Teen Girl Squad —— 最小树形图
题目链接:https://vjudge.net/problem/UVA-11183
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
题解:
最小树形图,即有向图的最小生成树。因为题目要求可以有重边,所以对于两个点,如果之间有多条边(有向边,u-->v),选取权值最小的那条边。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; struct Edge
{
int u, v, w;
}edge[]; int pre[MAXN], id[MAXN], vis[MAXN], in[MAXN]; int zhuliu(int root, int n, int m)
{
int res = ;
while()
{
for(int i = ; i<n; i++) //初始化
in[i] = INF;
for(int i = ; i<m; i++) //为每个结点选择一条最小入边, 并记录它的上一个点。
if(edge[i].u!=edge[i].v && edge[i].w<in[edge[i].v]) //第一个判断防止自环
{
pre[edge[i].v] = edge[i].u;
in[edge[i].v] = edge[i].w;
} for(int i = ; i<n; i++) //如果非根结点找不到至少一条入边,则建树失败
if(i!=root && in[i]==INF)
return -; int tn = ; //tn为重建图后的结点个数,过程中为结点重新编号
memset(id, -, sizeof(id));
memset(vis, -, sizeof(vis));
in[root] = ; //根节点的入度必须为0
for(int i = ; i<n; i++) //将环缩成点
{
res += in[i];
int v = i;
//当vis[v]==i时,找到了环中第一个被访问的结点
while(vis[v]!=i && id[v]==- && v!=root)
{
vis[v] = i;
v = pre[v];
}
if(v!=root && id[v]==-)
{
for(int u = pre[v]; u!=v; u = pre[u]) //为整个换编上号
id[u] = tn;
id[v] = tn++;
}
}
if(tn==) break; //如果不存在环, 则建树成功
for(int i = ; i<n; i++) //为不在环内的结点编号
if(id[i]==-)
id[i] = tn++; for(int i = ; i<m; ) //重新建图
{
int v = edge[i].v;
edge[i].u = id[edge[i].u];
edge[i].v = id[edge[i].v];
if(edge[i].u!=edge[i].v)
edge[i++].w -= in[v];
else
swap(edge[i], edge[--m]);
}
n = tn; //更新结点个数及根节点
root = id[root];
}
return res;
} int g[MAXN][MAXN];
int main()
{
int T, n, m;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d",&n,&m);
for(int i = ; i<n; i++)
for(int j = ; j<n; j++)
g[i][j] = INF; for(int i = ; i<m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u][v] = min(g[u][v], w);
} int tot = ;
for(int i = ; i<n; i++)
for(int j = ; j<n; j++)
{
if(i!=j && g[i][j]!=INF)
edge[tot].u = i, edge[tot].v = j, edge[tot++].w = g[i][j];
} int ans = zhuliu(, n, tot);
if(ans<) printf("Case #%d: Possums!\n", kase);
else printf("Case #%d: %d\n", kase, ans);
}
}
UVA11183 Teen Girl Squad —— 最小树形图的更多相关文章
- UVa11183 - Teen Girl Squad(最小树形图-裸)
Problem I Teen Girl Squad Input: Standard Input Output: Standard Output -- 3 spring rolls please. - ...
- UVA 11183 Teen Girl Squad 最小树形图
最小树形图模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <c ...
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
Teen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n teenage ...
- UVA-11183 Teen Girl Squad (最小树形图、朱刘算法模板)
题目大意:给一张无向图,求出最小树形图. 题目分析:套朱-刘算法模板就行了... 代码如下: # include<iostream> # include<cstdio> # i ...
- Uva 11183 - Teen Girl Squad (最小树形图)
Problem ITeen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n ...
- 最小树形图模板 UVA11183
题意:给定n个节点m条边的有向带权图,求以0为根节点的最小树形图权值大小 用这个代码的时候要注意,这里的数据是从0开始的,边也是从0开始算, 所以在打主代码的时候,如果是从1开始,那么算法里面的从0开 ...
- kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数
第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...
- UVA:11183:Teen Girl Squad (有向图的最小生成树)
Teen Girl Squad Description: You are part of a group of n teenage girls armed with cellphones. You h ...
- Codeforces 240E. Road Repairs 最小树形图+输出路径
最小树形图裸题,只是须要记录路径 E. Road Repairs time limit per test 2 seconds memory limit per test 256 megabytes i ...
随机推荐
- ES6(字符串)
ES6新增字符串特性 一.Unicode的表示法 当码值>2个字节(0xff) 即第一个数字未处理,不显示 处理这种超过2字节的情况,用{}包起来即可 二.API 1.ES5中 码值>2个 ...
- nmon分析与详解
1.命令安装 1.查看liunx版本版本x86_64_14i 目录:cd /nmon/logs/ 版本x86_64_14i [root@localhost u06]# cd / [root@local ...
- PTA 05-树8 File Transfer (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer (25分) We have a netwo ...
- HDU3572:Task Schedule【最大流】
上了一天课 心塞塞的 果然像刘老师那么说 如果你有挂科+4级没过 那基本上是WF队 题目大意:有时间补吧 思路:给每个任务向每个时间点连边容量为1 每个时间点向汇点连边 容量为机器的个数 源点向每个任 ...
- HDU 2352 Verdis Quo
罗马数字转化为十进制的值 题目非常的长 提取有效信息 并且介绍很多规则 但是事实上有用的信息就是如何加 什么时候减 当当前字母小于下一个字母时 减去当前字母的值 #include <iostre ...
- CF830B:Cards Sorting
对叠放着的n张牌,第i张牌写有数字Ai,进行操作:将牌堆顶的牌取出,若是当前牌堆最小值就扔掉,否则放到牌堆底,求牌堆空时操作次数. 怎么看怎么像约瑟夫..不过约瑟夫DP我不太熟,于是就yy了一下 “当 ...
- URAL 1614. National Project “Trams” [ 构造 欧拉回路 ]
传送门 1614. National Project “Trams” Time limit: 0.5 secondMemory limit: 64 MB President has declared ...
- iOS 如何查看崩溃日志
参考网址: [转载]https://www.jianshu.com/p/4de55d73c82b [转载]https://blog.csdn.net/qq_26544491/article/detai ...
- OC-Xcode中导入runtime框架,函数参数没有提示的处理方法
在了解runtime时,如果自己编写runtime代码,需要先导入头文件: #import <objc/message.h> 之后,例如了解runtime的消息机制时,调用objc_msg ...
- App竞品技术分析 (3)减小安装包的体积(转)
http://blog.csdn.net/JspAndAsp/article/details/49339403 1 从几件小事说起 春节在家帮姐姐的iPhone手机安装市面上形形色色的App,忘记她是 ...