Intelligence System

        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
              Total Submission(s): 2909    Accepted Submission(s): 1259

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3069 3077 3070 3071 3073 
 
思路:
 先tarjan缩点,然后贪心加边将这几个连通块连接起来
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 300010
using namespace std;
bool vis[N];
int n,m,x,y,z,s,tot,num,sum,ans,top,tim;
int fa[N],low[N],dfn[N],stack[N],head[N],belong[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,dis,next;
}edge[N];
struct Edde
{
    int x,y,z;
}edde[N];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
int add1(int x,int y,int z)
{
    s++;
    edde[s].x=x;
    edde[s].y=y;
    edde[s].z=z;
}
void begin()
{
    s=,tim=,sum=,tot=;
    memset(dfn,,sizeof(dfn));
    memset(low,,sizeof(low));
    memset(vis,,sizeof(vis));
    memset(head,,sizeof(head));
    memset(belong,,sizeof(belong));
}
void tarjan(int now)
{
    dfn[now]=low[now]=++tim;
    stack[++top]=now; vis[now]=true;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(vis[t]) low[now]=min(low[now],dfn[t]);
        else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
    }
    if(low[now]==dfn[now])
    {
        sum++;belong[now]=sum;
        for(;stack[top]!=now;top--)
        {
            int x=stack[top];
            belong[x]=sum;vis[x]=false;
        }
        vis[now]=false;top--;
    }
}
void shrink_point()
{
    ;i<=n;i++)
    {
        for(int j=head[i];j;j=edge[j].next)
         if(belong[i]!=belong[edge[j].to])
         {
             int t=edge[j].to;
             add1(belong[i],belong[t],edge[j].dis);
            add1(belong[t],belong[i],edge[j].dis);
          }
    }
}
int cmp(Edde a,Edde b)
{
    return a.z<b.z;
}
int find(int x)
{
    if(x==fa[x]) return x;
    fa[x]=find(fa[x]);
    return fa[x];
}
void kruskal()
{
    ans=,num=;
    ;i<=n;i++) fa[i]=i;
    sort(edde+,edde++s,cmp);
    ;i<=s;i++)
    {
        x=edde[i].x,y=edde[i].y;
        int fx=find(x),fy=find(y);
        if(fx==fy) continue;
        fa[fx]=fy;num++;
        ans+=edde[i].z;
        ) break;
    }
    printf("%lld\n",ans);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        begin();
        ;i<=m;i++)
          x=read(),y=read(),z=read(),add(x+,y+,z);
        ;i<=n;i++)
         if(!dfn[i]) tarjan(i);
        shrink_point();
        kruskal();
    }
    ;
}

tarjan+最小生成树

wa、、、、

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 300010
#define maxn 0x7fffffff
using namespace std;
bool vis[N];
long long ans;
int n,m,x,y,z,s,tot,num,sum,top,tim;
int fa[N],low[N],dfn[N],cost[N],stack[N],head[N],belong[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,dis,next;
}edge[N];
struct Edde
{
    int to,dis,next;
}edde[N];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
void begin()
{
    s=,tim=,sum=,tot=;
    memset(dfn,,sizeof(dfn));
    memset(low,,sizeof(low));
    memset(vis,,sizeof(vis));
    memset(head,,sizeof(head));
    memset(belong,,sizeof(belong));
}
void tarjan(int now)
{
    dfn[now]=low[now]=++tim;
    stack[++top]=now; vis[now]=true;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(vis[t]) low[now]=min(low[now],dfn[t]);
        else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
    }
    if(low[now]==dfn[now])
    {
        sum++;belong[now]=sum;
        for(;stack[top]!=now;top--)
        {
            int x=stack[top];
            belong[x]=sum;vis[x]=false;
        }
        vis[now]=false;top--;
    }
}
void work()
{
    ans=;
    ;i<=n;i++)
      for(int j=head[i];j;j=edge[j].next)
      {
          int t=edge[j].to;
          if(belong[i]!=belong[t])
            cost[belong[t]]=min(cost[belong[t]],edge[j].dis);
     }
   ;i<=sum;i++)
    if(cost[i]!=maxn) ans+=(long long)cost[i];
   printf("%lld\n",ans);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        begin();
        ;i<=n;i++) cost[i]=maxn;
        ;i<=m;i++)
          x=read(),y=read(),z=read(),add(x+,y+,z);
        ;i<=n;i++)
         if(!dfn[i]) tarjan(i);
        work();
    }
    ;
}

HDU——3072 Intelligence System的更多相关文章

  1. HDU 3072 Intelligence System (强连通分量)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. hdu 3072 Intelligence System(Tarjan 求连通块间最小值)

    Intelligence System Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  4. HDU - 3072 Intelligence System

    题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...

  5. HDU——T 3072 Intelligence System

    http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  6. hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. Intelligence System

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

  9. HDU 3072 SCC Intelligence System

    给出一个带权有向图,要使整个图连通.SCC中的点之间花费为0,所以就先缩点,然后缩点后两点之间的权值为最小边的权值,把这些权值累加起来就是答案. #include <iostream> # ...

随机推荐

  1. 我用 Python 爬了智联“北上广深”5400条 Java 招聘数据

    结论 国际惯例,先上结论. Java 类职位招聘,不论是需求量(工作机会),还是工资平均水平,都是帝都北京最好. 北京和上海的平均工资差距不大(不超过200/月),但上海的需求量是北京的一半,机会更少 ...

  2. scala基础篇-03 if与for

    一.Scala中的if是表达式** 1.定义方式 2.例子 二.for 的用法 1.定义方式: for{ x <- xs y=x+ ) }yield y 2.例子:

  3. R Programming week1-Reading Data

    Reading Data There are a few principal functions reading data into R. read.table, read.csv, for read ...

  4. MVC 附件在线预览

    原因:应客户需求,在系统中浏览附件内容,需要先下载到本地然后打开,对使用造成了不便,要求可以不需下载直接在浏览器中打开减少操作步骤. 领导给了3天时间,最后查找方法,写测试项目,往正式项目添加,测试, ...

  5. 数据层优化-jdbc连接池简述、druid简介

    终于回到既定轨道上了,这一篇讲讲数据库连接池的相关知识,线程池以后有机会再结合项目单独写篇文章(自己给自己挖坑,不知道什么时候能填上),从这一篇文章开始到本阶段结束的文章都会围绕数据库和dao层的优化 ...

  6. swift 与 @objc

    Objective-C entry points https://github.com/apple/swift-evolution/blob/master/proposals/0160-objc-in ...

  7. Okhttp3发送xml、json、文件的请求方法

    1.引入依赖 <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okht ...

  8. 表单文件上传编码方式(enctype 属性)

    enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 如下: <form action="upload.php" method="post&quo ...

  9. vue的[__ob__: Observer]

    为什么会获取不到里面的值 因为:vue data 里面值都是有这个属性的.这是被vue接管的数据,observer是Vue核心中最重要的一个模块(个人认为),能够实现视图与数据的响应式更新,底层全凭o ...

  10. c语言中的 strcpy和strncpy字符串函数使用介绍

    1.strcpy函数 函数原型:char *strcpy(char *dst,char const *src)            必须保证dst字符的空间足以保存src字符,否则多余的字符仍然被复 ...