首页 > ACM题库 > HDU-杭电 > HDU 3072-Intelligence System-贪心-[解题报告]HOJ
2014
03-01

HDU 3072-Intelligence System-贪心-[解题报告]HOJ

Intelligence System

问题描述 :

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!

输入:

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.

输出:

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.

样例输入:

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

样例输出:

150
100
50

强连通分量——tarjin 算法

这道题和前面那道hdu 2767唯一不同就是,2767需要找出最小数量的边使图成为连通分量,而这个题需要一点点贪心的思想在里面,它需要求出代价最小的边使图成为连通分量;

代码:

#include <cstdio>
 #include <cstring>
 #include <iostream>
 #include <stack>
 #define N 50006
 using namespace std;
 
 struct Edge
 {
     int u, val, next;
     Edge() {}
     Edge(int a, int b, int c)
     {
         u=a, val=b, next=c;
     }
 } edge[100006];
 
 int head[N],tot,n,m,dfn[N],low[N],T,ind,id[N],in[N];
 bool vs[N];
 stack<int> S;
 
 void add_edge(int st, int en, int val)
 {
     edge[tot]=Edge(en,val,head[st]);
     head[st]=tot++;
 }
 
 void tarjan(int u)
 {
     S.push(u), vs[u]=true;
     dfn[u]=low[u]=T++;
     for(int e=head[u]; e!=-1; e=edge[e].next)
     {
         int v=edge[e].u;
         if(!dfn[v])
         {
             tarjan(v);
             low[u]=min(low[u], low[v]);
         }
         else if(vs[v] && low[u]>dfn[v]) low[u]=dfn[v];
     }
     if(low[u]==dfn[u])
     {
         ind++;
         int v;
         do
         {
             v=S.top();
             S.pop();
             id[v]=ind;
             vs[v]=false;
         }while(v!=u);
     }
 }
 
 int main()
 {
     while(scanf("%d%d", &n, &m)!=EOF)
     {
         memset(head, -1, sizeof head);
         tot=0;
         for(int i=0, a, b, c; i<m; i++)
         {
             scanf("%d%d%d", &a, &b, &c);
             add_edge(a,b,c);
         }
         while(!S.empty()) S.pop();
         memset(vs, 0, sizeof vs);
         memset(dfn,0, sizeof dfn);
         memset(low,0,sizeof low);
         T=ind=0;
         for(int i=0; i<n; i++) if(!dfn[i]) tarjan(i);
         for(int i=0; i<ind; i++) in[i]=999999;
         for(int i=0; i<n; i++)
         {
             int u=id[i];
             for(int e=head[i]; e!=-1; e=edge[e].next)
             {
                 int v=id[edge[e].u];
                 if(u!=v) in[v]=min(in[v], edge[e].val);
             }
         }
         int ans=0;
         for(int i=0; i<ind; i++)
         {
             if(i==id[0]||in[i]==999999) continue;
             ans+=in[i];
         }
         printf("%d\n", ans);
     }
     return 0;
 }

 

参考:http://www.cnblogs.com/yours1103/archive/2013/09/04/3301907.html


  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。

  2. “再把所有不和该节点相邻的节点着相同的颜色”,程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的

  3. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }