Intelligence System (hdu 3072 强联通缩点+贪心)

Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1650    Accepted Submission(s): 722

Problem Description

After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it is of course for ACM ... ...

Now, kzc_tc, the head of the Intelligence Department (his code is once 48, but now 0), is sudden obtaining important information from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it
need for emergency notification to all Intelligence personnel, he decides to use the intelligence system (kzc_tc inform one, and the one inform other one or more, and so on. Finally the information is known to all).

We know this is a dangerous work. Each transmission of the information can only be made through a fixed approach, from a fixed person to another fixed, and cannot be exchanged, but between two persons may have more than one way for transferring. Each act of
the transmission cost Ci (1 <= Ci <= 100000), the total cost of the transmission if inform some ones in our ALPC intelligence agency is their costs sum.

Something good, if two people can inform each other, directly or indirectly through someone else, then they belong to the same branch (kzc_tc is in one branch, too!). This case, it’s very easy to inform each other, so that the cost between persons in the same
branch will be ignored. The number of branch in intelligence agency is no more than one hundred.

As a result of the current tensions of ALPC’s funds, kzc_tc now has all relationships in his Intelligence system, and he want to write a program to achieve the minimum cost to ensure that everyone knows this intelligence.

It‘s really annoying!

Input

There are several test cases.

In each case, the first line is an Integer N (0< N <= 50000), the number of the intelligence personnel including kzc_tc. Their code is numbered from 0 to N-1. And then M (0<= M <= 100000), the number of the transmission approach.

The next M lines, each line contains three integers, X, Y and C means person X transfer information to person Y cost C.

Output

The minimum total cost for inform everyone.

Believe kzc_tc’s working! There always is a way for him to communicate with all other intelligence personnel.

Sample Input

3 3
0 1 100
1 2 50
0 2 100
3 3
0 1 100
1 2 50
2 1 100
2 2
0 1 50
0 1 100

Sample Output

150
100
50

Source

2009 Multi-University Training Contest 17 - Host by NUDT

Recommend

lcy   |   We have carefully selected several similar problems for you:  3069 3077 3070 3071 3073

题意:n个人m个单向关系,现在要通知所有的人,两个人之间联系有费用,求最小费用,处于同一个联通块的两个人之间通讯不需要花费。

思路:先建图使用Tarjan算法缩点,然后根据题意我们应该求缩点后新图的最小树形图,但是这里没必要,为什么?仔细想一想,首先题意说总是有解,所以最小树形图一定存在,那么我们对于每一个点在它的所有入边中选择一个花费最小的入边(入度为零的点除外)那么答案就是每个点的最小花费之和。这样贪心是可行的,因为在这个过程中不会出现环,很容易想到,如果出现了环那么这个环就又是一个联通块了,可是我们之前已经求出了联通块,保证了新图中没有环。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

const int MAXN = 50010;//点数
const int MAXM = 500010;//边数

struct Edge
{
    int to,c,next;
}edge[MAXM];

int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
int Index,top;
int scc;//强联通分量的个数
bool Instack[MAXN];
int num[MAXN];//各个强联通分量包含的点的个数,数组编号为1~scc
//num数组不一定需要,结合实际情况

void addedge(int u,int v,int c)
{
    edge[tot].to=v;
    edge[tot].c=c;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void Tarjan(int u)
{
    int v;
    Low[u]=DFN[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;
    for (int i=head[u];i+1;i=edge[i].next)
    {
        v=edge[i].to;
        if (!DFN[v])
        {
            Tarjan(v);
            if (Low[u]>Low[v]) Low[u]=Low[v];
        }
        else if (Instack[v]&&Low[u]>DFN[v])
            Low[u]=DFN[v];
    }
    if (Low[u]==DFN[u])
    {
        scc++;
        do{
            v=Stack[--top];
            Instack[v]=false;
            Belong[v]=scc;
            num[scc]++;
        }while (v!=u);
    }
}

void solve(int N)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    memset(num,0,sizeof(num));
    Index=scc=top=0;
    for (int i=1;i<=N;i++)      //点的编号从1开始
        if (!DFN[i])
            Tarjan(i);
}

int n,m;
int d[MAXN],in[MAXN];

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(d,INF,sizeof(d));
    memset(in,0,sizeof(in));
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
    int i,j,u,v,c;
    while (~sff(n,m))
    {
        init();
        for (i=0;i<m;i++)
        {
            sfff(u,v,c);
            u++;v++;
            addedge(u,v,c);
        }
        solve(n);
        int ans=0;
        for (u=1;u<=n;u++)
        {
            for (i=head[u];~i;i=edge[i].next)
            {
                int v=edge[i].to;
                if (Belong[u]!=Belong[v])
                    in[Belong[v]]++;
            }
        }
        for (u=1;u<=n;u++)
        {
            for (j=head[u];~j;j=edge[j].next)
            {
                int v=edge[j].to;
                if (Belong[u]!=Belong[v])
                    d[Belong[v]]=min(d[Belong[v]],edge[j].c);
            }
        }
        for (i=1;i<=scc;i++)
        {
            if (in[i]==0) continue;
            ans+=d[i];
        }
        pf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 08:09:25

Intelligence System (hdu 3072 强联通缩点+贪心)的相关文章

Intelligence System HDU - 3072(强连通分量)

Intelligence System HDU - 3072 题意:一个人要传递命令给所有人,如果两人之间互达,不需任何费用,求最少费用 有向图强连通. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int inf=0x3f3f3f3f; 4 const int maxv=50010; 5 const int maxe=100010; 6 int n,m; 7 struct Edge{ 8 int u,v,w; 9 int

Proving Equivalences (hdu 2767 强联通缩点)

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3743    Accepted Submission(s): 1374 Problem Description Consider the following exercise, found in a generic linear algebra

Summer Holiday (hdu 1827 强联通缩点)

Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2054    Accepted Submission(s): 941 Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Inf

POJ 1236 Network of School(强联通缩点)

Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the "receiving schools"). Note that if B is in the

ZOJ 3795 Grouping 强联通缩点+拓扑序+偏序集的最大链的大小

题意:有n个人,m个关系,关系是这两个人前一个人可以跟后一个比较. 那么问你我最少分多少组可以使这个组里的人都不可以比较. 只会强联通缩点,真特么不知道怎么做,想了一个小时,网上一看,还要会偏序集的东西,有一个叫Dilworth定理的东西. 定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链. 其对偶定理称为Dilworth定理: 定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小.则X可以被划分成m个但不能再少的链. 然后我们用到的是

[bzoj 1093][ZJOI2007]最大半联通子图(强联通缩点+DP)

题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1093 分析: 首先肯定是先把强联通全部缩成一个点,然后成了一个DAG 下面要知道一点:原图的最大半联通子图实际是上是新DAG图的一个最长链 然后就像拓扑排序一样(不过这是以出度为0的点优先,拓扑排序以入度为0的点优先),设f[i]表示以节点i为起始节点的最长链的长度,s[i]表示以节点i为起始节点的最长链的条数,然后就DP一样搞…… 注意: 1.缩点的时候有可能有重边,要注意判断 2

HDU 2767 强联通分量

点击打开链接 题意:问加多少边后图会变成强联通分量为1的图 思路:简单的强联通,缩点后找入度和出度就行了,水题 #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; co

HDU 4685 强联通分量+网络流

点击打开链接 题意:与POJ 1904 极其相像的一道题目,POJ的将一个完备匹配图给了你,并给了你一组可能的情况,很简单,但是这道题目,给的既不是完备匹配也没有给出可行的匹配方案,难的不要不要的 思路:刚开始看以为是和1904一模一样呢,然而难度上升的真快,看了一下是13年的多校题目,过了10几个把,可想而知这难度不是我等能够A掉的,刚自己想的是只加王子使其变成完备匹配,然后WA了,对了说一下为什么非要变成完备匹配呢,因为这道题目里的王子和公主的数量并不一定相同,就算相同也有可能不是完备匹配,

2017 Multi-University Training Contest - Team 9 1005&amp;&amp;HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】

FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1060    Accepted Submission(s): 506 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any oth