【UVA 11183】 Teen Girl Squad (定根MDST)
【题意】
输入三元组(X,Y,C),有向图,定根0,输出MDST。
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
【分析】
裸的MDST。
哇,自己打一下真是各种bug orz。。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010
#define Maxm 40010
#define INF 0xfffffff struct node
{
int x,y,c;
}t[Maxm]; int in[Maxn],vis[Maxn],id[Maxn],pre[Maxn]; int n,m,rt; int MDST()
{
int ans=;
rt=;
while()
{
for(int i=;i<=n;i++) in[i]=INF;
for(int i=;i<=m;i++)
{
int x=t[i].x,y=t[i].y;
if(t[i].c<in[y]&&x!=y)
{
in[y]=t[i].c;
pre[y]=x;
}
}
for(int i=;i<=n;i++) if(i!=rt&&in[i]==INF) return -;
memset(vis,-,sizeof(vis));
memset(id,-,sizeof(id));
int cnt=;
for(int i=;i<=n;i++) if(i!=rt)
{
ans+=in[i];
int now=i;
while(vis[now]!=i&&id[now]==-&&now!=rt)
{
vis[now]=i;
now=pre[now];
}
if(now!=rt&&id[now]==-)
{
cnt++;
for(int j=pre[now];j!=now;j=pre[j])
id[j]=cnt;
id[now]=cnt;
}
}
if(cnt==) break;
for(int i=;i<=n;i++) if(id[i]==-) id[i]=++cnt;
for(int i=;i<=m;i++)
{
int x=t[i].x,y=t[i].y;
t[i].x=id[x];t[i].y=id[y];
if(t[i].x!=t[i].y) t[i].c-=in[y];
}
rt=id[rt];
n=cnt; }
return ans;
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].c);
t[i].x++;t[i].y++;
}
printf("Case #%d: ",++kase);
int x=MDST();
if(x==-) printf("Possums!\n");
else printf("%d\n",x);
}
return ;
}
2016-11-01 14:42:51
【UVA 11183】 Teen Girl Squad (定根MDST)的更多相关文章
- 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 (有向图的最小生成树)
Teen Girl Squad Description: You are part of a group of n teenage girls armed with cellphones. You h ...
- 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 ...
- 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<cstdio> # i ...
- UVA11183 Teen Girl Squad —— 最小树形图
题目链接:https://vjudge.net/problem/UVA-11183 You are part of a group of n teenage girls armed with cell ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
随机推荐
- Scala函数字面量简化写法
Scala提供了多种方法来简化函数字面量中多余的部分,比如前面例子中filter方法中使用的函数字面量,完整的写法如下: (x :Int ) => x +1 首先可以省略到参数的类型,Scala ...
- C#垃圾回收机制详解
一.托管代码/非托管代码 C#代码通过C#编译器编译成程序集,程序集由微软中间语言组成,CLR会为程序集开辟一个应用程序域,程序集就是运行在这个应用程序域里面的,应用程序域是相互独立的,互不影响. 托 ...
- Android开发5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...
- HttpServlet was not found on the Java
今天新建jsp时出现了一个错误,如下图 分析:应该是没有找到相关jar包 解决方案: 如图: 这回就没错了
- ADB操作多台设备
1.adb devices 查看所有连接设备. 2.adb -s <设备名> shell 指定device来执行adb shell. 3.adb -s <设备名> <指令 ...
- Android colors.xml
<?xml version="1.0" encoding="utf-8"?><resources> <color name=&qu ...
- ios隐藏导航栏底线条和导航、状态栏浙变色
方法一遍历法: 在你需要隐藏的地方调用如下代码 [self findlineviw:self.navigationBar].hidden = YES; -(UIImageView*)findlinev ...
- LA 3902 Network(树上最优化 贪心)
Network Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes ...
- 005 Python的数值类型
005 Python的数值类型 BIF 指的是内置函数,一般不作为变量命名.如 input,while,if,else,float,等等.整型:整数.(python3.0版本把整型和长整型结合在 ...
- PHP下拉框选择的实现方法
实现 第一种PHP下拉框实现方法: < ?php //提交下拉框; //直接饱触发onchange事件的结果 $id=$_GET['myselect']; // myselect 为locati ...