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 ...
随机推荐
- 使用PL/SQL将sql脚本数据导入数据库
一. PL/SQL登录到数据库,使用tools工具进行导入.使用plsql登录到需要导入数据的数据库.点击工具栏上[tools]--[Import tables] 二.commit;
- python 监控oracle 数据库
import cx_Oracle import os db = cx_Oracle.connect('**********') print "Show Oracle Version: &qu ...
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- UML的关联(Association), 聚合(Aggregation), 组合(Composition)区别
转载:http://blog.csdn.net/ocean181/article/details/6117369 UML的关联(Association), 聚合(Aggregation), 组合(Co ...
- MT6755 使用R63350 IC 出现唤醒概率性闪白,并导致ESD FAIL
现象描述. 手机自动灭屏后按power键或home 键点亮屏幕,概率性上方有白色的一道,还会闪两三下屏.使用的LCM IC是:r63350, (FHD VDO)屏,附件为mtklog看看是什么原因? ...
- 【HDOJ6333】Harvest of Apples(莫队)
题意: 给定T组询问,每组有两个数字n和m,求sigma i=0..m c(n,i) 答案对1e9+7取模 T<=1e5 1<=n,m<=1e5 思路: 注意要先变n再变m,否则会因 ...
- 前端学习之--CSS
CSS 常用帮助文档 CSS:被用来同时控制多重网页的样式和布局.HTML页面中CSS样式的写法有3种: 1:标签中写入 <body style='margin o auto;'> 2:h ...
- 洛谷 P3807 【模板】卢卡斯定理
P3807 [模板]卢卡斯定理 题目背景 这是一道模板题. 题目描述 给定n,m,p(1\le n,m,p\le 10^51≤n,m,p≤105) 求 C_{n+m}^{m}\ mod\ pCn+mm ...
- 代码svn下载到本地后,关于数据库问题
代码svn下载到本地后,关于数据库问题 1.那我本地还用搭建相应的数据库么?答案:当然不用啦,本地系统里已经配置好了数据库的网络地址了,端口号,密码啥的.即使你代码运行在本地,依然可以将数据传输到服务 ...
- html5开发手机打电话发短信功能
原文:http://www.open-open.com/code/view/1449843459332 在很多的手机网站上,有打电话和发短信的功能,对于这些功能是如何实现的呢.其实不难,今天我们就用h ...