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

Teen Girl Squad

Input: Standard Input

Output: Standard Output

You are part of a group of n teenage girls armed with cellphones. You have some news you want to tell everyone in the group. The problem is that no two of you are in the same room, and you must communicate using only cellphones. What‘s worse
is that due to excessive usage, your parents have refused to pay your cellphone bills, so you must distribute the news by calling each other in the cheapest possible way. You will call several of your friends, they will call some of their friends, and so on
until everyone in the group hears the news.

Each of you is using a different phone service provider, and you know the price of girl A calling girl B for all possible A and B. Not all of your friends like each other, and some of them will never call people they don‘t like. Your job is to find the cheapest
possible sequence of calls so that the news spreads from you to all n-1 other members of the group.

Input

The first line of input gives the number of cases, (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, uv 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    Sample Output

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
Case #1: 10
Case #2: Possums!
Case #3: 40
Case #4: 130
                  


最小树形图

有向图的最小生成树,并且规定了起点。

解法:1.首先dfs判断一下起点可达其他任意点,否则不存在树形图。

2.为每个点找一条最小的入边,如果没环那么这些边就构成了最小树形图,转入4;否则转入3.

3.将环上每条边的边权加入到ans中,同时形成新的点new,对于环内多有的点i,如果存在边<j,i>则<j,new>的边权等于所有 <j,i>-<pre[i],i>中最小的(因为缩点后再次构图必须从环中去除一条边<pre[i],i>再添加一条最小边<x,i>,这样就可以保证答案的正确性,很巧妙,换个图就很清晰了),<new,j>的边权=所有<i,j>的最小值,缩点完成,转向2.

4.缩点后n个点,n-1条边,切无环,此时就是一颗连通树了,ans+=这n-1条边的边权记得答案;

以上是国人发明的“朱刘算法”,邻接矩阵复杂度(n ^3)临界表复杂度(VE)。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#define for0(a,b) for(a=0;a<b;++a)
#define for1(a,b) for(a=1;a<=b;++a)
#define foru(i,a,b) for(i=a;i<=b;++i)
#define ford(i,a,b) for(i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const int maxn = 1000 + 5;
const int maxm = 40000 + 5;
const int INF = 1e9;

struct Edge{ int u, v, cost;};
Edge edge[maxm];
int pre[maxn], id[maxn], vis[maxn], in[maxn];
int zhuliu(int root, int n, int m, Edge edge[])
{
    int res = 0, u, v;
    int i, j;
    while(1){
        for0(i,n) in[i] = INF;
        for0(i,m) if(edge[i].u != edge[i].v && edge[i].cost < in[edge[i].v]){
            pre[edge[i].v] = edge[i].u;
            in[edge[i].v] = edge[i].cost;
        }
        for0(i,n) if(i != root && in[i] == INF) return -1;//不能存在最小树形图
        int tn = 0;
        memset(id, -1, sizeof id );
        memset(vis, -1, sizeof vis );
        in[root] = 0;
        for0(i,n)
        {
            res += in[i];
            v = i;
            while(vis[v] != i && id[v]==-1 && v!=root){
                vis[v] = i;
                v = pre[v];
            }
            if(v != root && id[v] == -1){
                for(int u=pre[v]; u != v; u = pre[u])
                    id[u] = tn;
                id[v] = tn++;
            }
        }
        if(tn==0) break; //没有有向环
        for0(i,n) if(id[i] == -1)
            id[i] = tn++;
        for(i=0; i<m; )
        {
            v = edge[i].v;
            edge[i].u = id[edge[i].u];
            edge[i].v = id[edge[i].v];
            if(edge[i].u != edge[i].v)
                edge[i++].cost -= in[v];
            else
                swap(edge[i], edge[--m]);
        }
        n = tn;
        root = id[root];
    }
    return res;
}

int g[maxn][maxn];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.cpp","r",stdin);
    freopen("out.cpp", "w", stdout);
#endif // ONLINE_JUDGE
    int n, m;
    int T, i, j;
    scanf("%d", &T);
    for(int cas=1; cas<=T; ++cas)
    {
        scanf("%d%d", &n, &m);
        for0(i,n) for0(j,n)
            g[i][j] = INF;
        int u, v, c;
        while(m--)
        {
            scanf("%d%d%d", &u, &v, &c);
            if(u==v) continue;
            g[u][v] =  min(g[u][v], c);
        }
        int e = 0;
        for0(i,n) for0(j,n) if(g[i][j]<INF){
            edge[e].u = i;
            edge[e].v = j;
            edge[e++].cost = g[i][j];
        }
        int ans = zhuliu(0, n, e, edge );
        printf("Case #%d: ", cas);
        if(ans == -1) printf("Possums!\n");
        else printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-11 05:11:40

UVa11183 Teen Girl Squad, 最小树形图,朱刘算法的相关文章

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

Problem I Teen Girl Squad Input: Standard Input Output: Standard Output -- 3 spring rolls please. -- MSG'D!! -- Oh! My stomach lining! Strong Bad You are part of a group of n teenage girls armed with cellphones. You have some news you want to tell ev

POJ 3164 Command Network 最小树形图-朱刘算法裸题

题目来源:POJ 3164 Command Network 题意:求以1为根的最小树形图 没有输出字符串 思路:直接高朱刘算法 不懂的可以百度 学会了就是直接套模板的事情 其实就是不断消圈而已 不构成圈就有解 无法从根到达其他点就无解 #include <cstdio> #include <cstring> #include <cmath> const int maxn = 110; const int maxm = 50010; const double INF =

POJ 3164 Command Network (最小树形图-朱刘算法)

题目地址:POJ 3164 最小树形图第一发. 把一个v写成u了.....TLE了一晚上...(虽说今晚出去玩了..) 刚开始看这个算法的时看模板以为又是一个isap....吓得一个哆嗦.但是仔细看了看之后发现还是挺好理解的.写下自己的理解. 朱刘算法其实只有3步,然后不断循环. 1:找到每个点的最小入边.既然是生成树,那么对于每个点来说,只要选一个权值最小的入边就可以了.贪心思想.因为如果不是最小入边,那么它肯定不是最小树形图的一条边,考虑它是没有意义的. 2:找环.找环找的是最小入边构成的新

图论--最小树形图朱刘算法模板

最小树形图就是一个有向图,从根节点可以到达其他所有节点,而除根节点外的每个节点又有且仅有一个父节点,这样一张边权和最小的图就是最小树形图. 最小树形图有它特有的算法,朱刘算法.原理是先对除根节点以外的所有节点先找寻一条最小入边,接着判图中是否有有向环,如果有,将每个环缩成一点,再对这些点重新找最小入边,重复合并操作直至没有环. 我用的主要是从kuangbin巨巨的模板小改得到的昂. 各种注释便于理解: 1 #include<stdio.h> 2 #include<string.h>

&ldquo;Hdu4966&rdquo; GGS-DDU - 最小树形图/朱刘算法

原文引用https://www.dazhuanlan.com/2019/08/26/5d6302f8c23db/ 题目链接:传送门 Description 有一个人,想学习N个科目,每个科目都有相应的层次 有M个课程,M个课程的要求是,你的第c个科目的层次要达到l1,才可以参加,参加完这个课程后,你需要缴费money,但你的第d个科目的层次会达到l2 问如何花最少的钱,使得每个科目的层次都达到最高 Solution 每个科目的每个层次都看成一个点,每个科目的层次0和一个根连边,费用为0:每个科目

(最小树形图 朱刘算法) poj 3164

Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 14340   Accepted: 4118 Description After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent a

HDU 4966 GGS-DDU (最小树形图-朱刘算法)

题目地址:HDU 4966 刚开始没看清总级别只有500这一条件,看成了每一个都是500..然后建图思路就想歪了.....后来才发现是总共只有500..那么建图就很简单了..把每个科目的每个等级都设为一个点,把所有的0等级设为同一个树根.然后把所有科目的高等级向低等级连一条权值为0的有向边,第一个作用是保证最后的最小树形图是所有点都可达,第二个作用是保证每节课的的所需等级,只要达到高等级,那么使低等级也符合.然后再对每节课连边即可. 代码如下: #include <iostream> #inc

TJU 2248 Channel Design (最小树形图-朱刘算法)

题目地址:TJU 2248 最小树形图模板题.熟练一下模板. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>

POJ - 3164-Command Network 最小树形图——朱刘算法

POJ - 3164 题意: 一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 题目就是求这个最小的树形图. 参考资料:https://blog.csdn.net/txl199106/article/details/62045479 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <strin

定根最小树形图 朱刘算法 luogu P4716

https://www.luogu.org/problem/P4716 #include<bits/stdc++.h> #define ll long long #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) #define per(ii,a,b) for(int ii=b;ii>=a;--ii) #define forn(i,x,g,e) for(int i=g[x];i;i=e[i].next) #define IO ios::sync