【题意】

  输入三元组(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. ArrayList的深度copy和浅度拷贝

    ArrayList的浅度拷贝方式: 通过Collections.copy方法实现浅度拷贝 ArrayList<GuideGroup> questionGuideGroupList = ne ...

  2. Java 并发——多线程基础

    Thead类与Runnable接口 Java的线程,即一个Thread实例. Java的线程执行过程有两种实现方式: 子类继承Thread类,并且重写void run()方法. 自定义类实现Runna ...

  3. Oracle删除多张表

    项目中遇到要删除多张表,发现不能同时删除,可以先查询出SQL语句,然后批量执行 1.查询出SQL语句: select 'drop table '||table_name || ';' from use ...

  4. YII设置用户访问过滤

    设置用户访问过滤 1.哪个控制器设置访问过滤,就在哪个控制器中添加如下代码 class XxxController extends Controller { //当前控制器是否使用过滤功能 publi ...

  5. CF-gym-100523-C(水题)

    Will It Stop? Available memory: 64 MB. Byteasar was wandering around the library of the University o ...

  6. javascript-02

    1.js的特点2.js的数据类型3.js运算符 4.js的全局变量   |-定义在函数体外部的变量   |-定义在函数体内部没有使用var声明 var和没有var声明变量的区别?     |-var ...

  7. jquery 可拖动进度条

    实现这个效果怎么弄呢? <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...

  8. order by跟group by 跟having(2)

  9. 3d max地形建造

    这里来记录一下max里面建造一个地形. 1.创建一个平面,调节平面的属性,包括长宽,和分段 2.然后建造一个道路 然后选择样条线工具,调节线条的轮廓. 3.使用合并工具,将线条和地面进行合并成为一个物 ...

  10. OLEDB读取EXCEL表格时,某些字段为空,怎么办?

    转载:http://blog.sina.com.cn/s/blog_53864cba01011cbn.html   前些日子,写了一个Excel导入数据库的共同Batch,突然有一天发现当我修改Exc ...