题意:

有一个女孩,需要打电话让所有的人知道一个消息,消息可以被每一个知道消息的人传递。

打电话的关系是单向的,每一次电话需要一定的花费。

求出打电话最少的花费或者判断不可能让所有人知道消息。

思路:

最小树形图模板题。

朱刘算法,复杂度O(n^3),n的规模较小。

代码:

 #include <stdio.h>
#include <string.h>
#include <vector>
using namespace std; const int N = ;
const int inf = 0x3f3f3f3f; struct edge
{
int from,to,cost; edge(int a,int b,int c)
{
from = a;
to = b;
cost = c;
}
}; int in[],pre[],vis[],id[]; vector<edge> es; int zhu_liu(int n)
{
int res = ; int root = ; while ()
{
memset(vis,-,sizeof(vis));
memset(id,-,sizeof(id));
memset(in,inf,sizeof(in)); for (int i = ;i < es.size();i++)
{
edge e = es[i]; int to = e.to; if (e.from != e.to && e.cost < in[to])
{
in[to] = e.cost;
pre[e.to] = e.from;
}
} for (int i = ;i < n;i++)
{
if (i != root && in[i] == inf) return -;
} in[root] = ; int cnt = ; for (int i = ;i < n;i++)
{
res += in[i]; int v = i; while (vis[v] != i && id[v] == - && v != root)
{
vis[v] = i;
v = pre[v];
} if (id[v] == - && v != root)
{
for (int u = pre[v];u != v;u = pre[u])
{
id[u] = cnt;
} id[v] = cnt++;
}
} if (cnt == ) break; for (int i = ;i < n;i++)
{
if (id[i] == -) id[i] = cnt++;
} for (int i = ;i < es.size();i++)
{
edge e = es[i]; int v = e.to; es[i].to = id[es[i].to];
es[i].from = id[es[i].from]; if (es[i].to != es[i].from) es[i].cost -= in[v];
} root = id[root];
n = cnt;
} return res;
} int main()
{
int t; int kase = ; scanf("%d",&t); while (t--)
{
int n,m; es.clear(); scanf("%d%d",&n,&m); for (int i = ;i < m;i++)
{
int a,b,c; scanf("%d%d%d",&a,&b,&c); es.push_back(edge(a,b,c));
} int ans = zhu_liu(n); printf("Case #%d: ",++kase); if (ans == -) printf("Possums!\n");
else printf("%d\n",ans);
} return ;
}

uva 11183 Teen Girl Squad的更多相关文章

  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. UVa11183 Teen Girl Squad, 最小树形图,朱刘算法

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

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

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

  6. 【UVA 11183】 Teen Girl Squad (定根MDST)

    [题意] 输入三元组(X,Y,C),有向图,定根0,输出MDST. InputThe first line of input gives the number of cases, N (N < ...

  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. 根据后台加载数据,添加loading动画

    <script> var current = 0; var hit = @hits; $(this).scroll(function(){ var viewHeight =$(this). ...

  2. java 流程控制--猜数字

    import java.util.Scanner; import java.util.Random; public class GuessNum{ public static void main(St ...

  3. nginx 负载均衡5种配置方式

    nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成正比, ...

  4. 【Selenium】selenium中隐藏元素如何定位?

    前言 面试题:selenium 中隐藏元素如何定位?这个是很多面试官喜欢问的一个题,如果单纯的定位的话,隐藏元素和普通不隐藏元素定位没啥区别,用正常定位方法就行了 但是吧~~~很多面试官自己都搞不清楚 ...

  5. 前端 HTML 常用标签 head标签相关内容 style标签 定义内部样式表

    styple标签 <!-- 定义内部样式表 --> <style type="text/css"></style>

  6. SQLite数据库管理工具(SQLiteStudio)v3.1.1

    http://www.pc6.com/softview/SoftView_86552.html

  7. Openresty 安装第三方插件

    Openresty 安装第三方插件 程序媛没有夜生活 2016.08.02 15:33* 字数 167 阅读 1283评论 0喜欢 2 在安装之前,我们先来看一下我们现有的模块. 1.将需要安装的插件 ...

  8. 解决PuTTY中文乱码

    转载:http://lhdeyx.blog.163.com/blog/static/3181969720091115113716947/ 打开putty,选择 Category中的Windows--- ...

  9. 利用Tensorflow实现神经网络模型

    首先看一下神经网络模型,一个比较简单的两层神经. 代码如下: # 定义参数 n_hidden_1 = 256 #第一层神经元 n_hidden_2 = 128 #第二层神经元 n_input = 78 ...

  10. php 获取 mime type 类型,fileinfo扩展

    背景: version < php-5.3 没有API能够查看文件的 mime_type, 故需要编译扩展 fileinfo 来扩展PHP的API(finfo_*系列函数).php-5.3 以后 ...