UVa11183 - Teen Girl Squad(最小树形图-裸)
Problem I
Teen Girl Squad
Input: Standard Input
Output: Standard Output
-- 3 spring rolls please. |
Strong Bad
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) andm (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 Output for 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 |
Case #1: 10 Case #2: Possums! Case #3: 40 Case #4: 130 |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1000+10;
const int inf = 1<<25;
struct edge{
int u,v,w;
edge(int u,int v,int w):u(u),v(v),w(w){}
};
vector<edge>e;
int n,m;
int pre[maxn],inv[maxn],to[maxn],ID[maxn]; int ZhuLiu(int rt){
int ret = 0;
while(true){
for(int i = 0; i < n; i++){
inv[i] = inf;
ID[i] = -1;
to[i] = -1;
}
for(int i = 0; i < m; i++){
int u = e[i].u,v = e[i].v,w = e[i].w;
if(inv[v] > w && u != v){
inv[v] = w;
pre[v] = u;
}
}
inv[rt] = 0;
for(int i = 0; i < n; i++){
if(inv[i] == inf) return -1;
}
int cnt = 0;
for(int i = 0; i < n; i++){
ret += inv[i];
int v = i;
while(to[v] != i && ID[v]==-1 && v != rt){
to[v] = i;
v = pre[v];
}
if(ID[v]==-1 && v != rt){
for(int u = pre[v]; u != v; u = pre[u]) ID[u] = cnt;
ID[v] = cnt++;
}
}
if(cnt==0) break;
for(int i = 0; i < n; i++){
if(ID[i]==-1) ID[i] = cnt++;
}
for(int i = 0; i < m; i++){
int u = e[i].u,v = e[i].v, w = e[i].w;
e[i].w -= inv[v];
e[i].u = ID[u];
e[i].v = ID[v];
}
rt = ID[rt];
n = cnt; }
return ret; }
int main(){
int ncase,T=1;
cin >> ncase;
while(ncase--){
e.clear();
scanf("%d%d",&n,&m);
for(int i = 0; i < m; i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e.push_back(edge(u,v,w));
}
int ans = ZhuLiu(0);
if(ans==-1){
printf("Case #%d: Possums!\n",T++);
}else{
printf("Case #%d: %d\n",T++,ans);
} }
return 0;
}
UVa11183 - Teen Girl Squad(最小树形图-裸)的更多相关文章
- UVA11183 Teen Girl Squad —— 最小树形图
题目链接:https://vjudge.net/problem/UVA-11183 You are part of a group of n teenage girls armed with cell ...
- 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 ...
- Codeforces 240E. Road Repairs 最小树形图+输出路径
最小树形图裸题,只是须要记录路径 E. Road Repairs time limit per test 2 seconds memory limit per test 256 megabytes i ...
- POJ3436 Command Network [最小树形图]
POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...
- 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上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...
随机推荐
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- DNS隧道工具使用 不过其网络传输速度限制较大
DNS隧道工具使用 http://www.freebuf.com/sectool/112076.html http://netsec.ccert.edu.cn/zhengming/2011/11/01 ...
- 【POJ 3700】 Missile Defence System
[题目链接] http://poj.org/problem?id=3700 [算法] 对于每一枚导弹,有4种决策 : 1.新建一套递增的系统拦截它 2.新建一套递减的系统拦截它 3.在已经建好的递增拦 ...
- [xPlugins] jQuery Contextmenu右键菜单
[2012-04-12] Contextmenu 右键菜单 v0.1 版本发布 [功能] 在特定区域弹出右键菜单 [功能] 可以在弹出右键菜单区域内,再屏蔽某个小区域. [功能] 有两种方式添加右键菜 ...
- TestNG环境搭建以及框架初识
TestNG的英文为Test Next Generation, 听上去好像下一代测试框架已经无法正常命名了的样子,哈哈,言归正传,啥是TestNG呢,它是一套测试框架,在原来的Junit框架的思想基础 ...
- 部署hexo后github pages页面未更新或无法打开问题
title: 部署hexo后github pages页面未更新或无法打开问题 date: 2018-03-30 15:34:29 categories: methods tags: hexo gith ...
- Sql中Convert日期格式
CONVERT(data_type,expression[,style]) convert(varchar(10),字段名,转换格式) 说明:此样式一般在时间类型(datetime,smalldate ...
- merge into语句的使用
merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入. 其基本语法规则是 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a ...
- c++中的强制转换
一.C语言的强制转换1.1 隐性转换 不同数据类型之间赋值和运算,函数调用传递参数等等,由编译器完成 int nTmp = 10; short sTmp ...
- 八叉树(Octree)Typescript 实现
Demo GitHub export class Octree { // 父&子树 private parent_node: any; private children_nodes: Octr ...