题意:

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

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

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

思路:

最小树形图模板题。

朱刘算法,复杂度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. jmeter_用户并发登录

    部分摘自:https://blog.csdn.net/weixin_41291554/article/details/80492276 第一种方案:对登录账号和密码进行参数化 1.添加设置线程数: N ...

  2. 1、python同级目录及子目录模块引入

    2个模块在同一个包内时(即引入和被引入的2个py文件在同一个目录下),直接引入模块名 1.引入与被引入模块或包在同一目录下时,直接引入模块名或者包名import modulename.py或者impo ...

  3. Vue项目

    1.新建Vue项目:vue init webpack projectName 2.vue-router模块 1.安装vue-router模块:npm install vue-router --save ...

  4. Servlet----------ServletConfig

    1. 什么是 ServletConfig servletConfig对象:用于封装servlet的配置信息.从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对servlet自身有效 ...

  5. max_allowed_packet引起同步报错处理

    一台MySQL的Cat数据库,每天早上1点定期删除,有4个表,删除完后,这4个表都有blob字段,很大量,部署删除job就同步报错. Got fatal error 1236 from master ...

  6. aop 日志统一处理

    AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理.缓存.对象池管理以及日志记录.AOP将这些分散在各 ...

  7. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. Laravel传值总结

    Laravel传值:with,view(),compact方法一:with public function index() { $title = '文章标题1'; return view('artic ...

  9. React.createClass 、React.createElement、Component

    react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...

  10. Stacking调参总结

    1. 回归 训练了两个回归器,GBDT和Xgboost,用这两个回归器做stacking 使用之前已经调好参的训练器 gbdt_nxf = GradientBoostingRegressor(lear ...