题意:

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

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

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

思路:

最小树形图模板题。

朱刘算法,复杂度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. jquery 数组的操作

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...

  2. 如何使用CryptoJS配合Java进行AES加密和解密

    注意 1. PKCS5Padding和PKCS7Padding是一样的 2. 加密时使用的key和iv要转换成base64格式 一.前端 1.函数 function encrypt (msg, key ...

  3. 数据库级别DML操作监控审计、表触发器/对象触发器

    使用触发器记录DML,使用触发器记录表的DML 数据库级别DML操作监控审计.表触发器/对象触发器 --创建记录表 CREATE TABLE T_SHALL_LOG ( ID , ) , EVTIME ...

  4. 文件压缩:zip

    [root@localhost ~]# yum install -y zip unzip // 安装 zip 和 unzip [root@localhost ~]# ..txt // 压缩文件,要同时 ...

  5. laravel项目出现Non-static method Redis::hGet() cannot be called statically的解决方法

    早上ytkah在配置laravel项目中出现Non-static method Redis::hGet() cannot be called statically错误提示,很显然这是redis出问题了 ...

  6. 帝国cms添加修改会员字段时字段名不能带数字,否则注册页会出现空白

    这几天ytkah在整帝国cms会员模块,根据客户需求添加不同的字段,这个相对不难,可还是遇到了点问题.当时添加会员字段时,在字段名用数字“1”来代表第一次,如下图的字段名“1rwsdy” 但是添加以后 ...

  7. 各大知名区块链交易所链接及API文档链接

    区块链交易所链接 火币网(Huobi):https://www.huobi.br.com/zh-cn/ API文档:https://github.com/huobiapi/API_Docs/wiki ...

  8. 为chrome设置代理

    1:打开google>setting>proxy  ,点击局域网设置. 2: 设置代理,当使用代理访问不了公司的网络时,需要将代理勾掉,将上面的公司用的网选上.

  9. 阿里云ECS利用密钥对ssh登录服务器

    https://blog.csdn.net/u012865381/article/details/78521087/ 1.在服务机上操作创建要远程登录的用户和密码 [root@izwz97s23bov ...

  10. 全连接与softmax[转载]

    转自:https://www.jianshu.com/p/88bb976ccbd9 1.全连接示例: 2.softmax softmax输入层应和输出层(输出维度与类别数一致)纬度一样,如果不一样,就 ...