【题意】

  输入三元组(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)的更多相关文章

  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 (有向图的最小生成树)

    Teen Girl Squad Description: You are part of a group of n teenage girls armed with cellphones. You h ...

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

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

  4. uva 11183 Teen Girl Squad

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

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

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

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

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

  7. UVA-11183 Teen Girl Squad (最小树形图、朱刘算法模板)

    题目大意:给一张无向图,求出最小树形图. 题目分析:套朱-刘算法模板就行了... 代码如下: # include<iostream> # include<cstdio> # i ...

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

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

  9. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

随机推荐

  1. 推荐几个对Asp.Net开发者比较实用的工具

    推荐几个对Asp.Net开发者比较实用的工具.大家有相关工具也可以在评论区留言,一起努力学习. 工具 1.Visual stdio Productivity Power tool:visual std ...

  2. ecshop在PHP 5.4以上版本各种错误问题处理

    在php5.4版本之后有很多的函数与功能进行丢弃与升级功能了,现在国内很多CMS都还未按php5.4标准来做了,下面我整理了一些在ecshop在PHP 5.4以上版本各种错误问题处理. 1.PHP 5 ...

  3. 关于MDCSwipeToChooseView的应用

    本人因为项目中某个页面的功能需要,用到了MDCSwipeToChooseView,就在网上查阅了相关的资料,资源有很多,但应该都是同一个人上传的,code4还有git上都有,但下载demo下来后运行不 ...

  4. OC与Swift的区别三(条件语句)

    11.swift中的switch结构 区别一: oc中switch条件只可以放整数 swift中switch条件可以放几乎任何数据类型 区别二: oc中每一个case中应有break,如果没有brea ...

  5. javascript 将多维数组转换为一维数组

    /** * 2013年9月去面试的时候,有面试过这样子一道题目: * 题目是这样子的:将一个多维数组转换成一维数组并返回该数组,类似 * [1,2,3,[4,5,6,[7,8]],9]转换后为:[1, ...

  6. 16_AOP入门准备_动态代理模式

    [工程截图] [PersonDao.java] package com.HigginCui.daoProxy; //目标类接口 public interface PersonDao { public ...

  7. IF EXIST: The syntax of the command is incorrect.

    Batch 脚本中使用 IF EXIST 语句时,总是提示 The syntax of the command is incorrect. 原始 bat 脚本如下: ECHO OFF SET proj ...

  8. C++学习指南

    转载于stackoverflow:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list 感谢Ge ...

  9. 九度OJ 1084 整数拆分

    题目地址:http://ac.jobdu.com/problem.php?pid=1084 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 ...

  10. 切换到mint了,纪念一下