HDU 1827 Tarjan

Summer Holiday

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2343    Accepted Submission(s): 1098


Problem Description
To see a World in a Grain of Sand 
And a Heaven in a Wild Flower, 
Hold Infinity in the palm of your hand 
And Eternity in an hour. 
                  —— William Blake

听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间和电话费了。他知道其他人也有一些别人的联系方式,这样他可以通知其他人,再让其他人帮忙通知一下别人。你能帮Wiskey计算出至少要通知多少人,至少得花多少电话费就能让所有人都被通知到吗?
 

Input
多组测试数组,以EOF结束。
第一行两个整数N和M(1<=N<=1000, 1<=M<=2000),表示人数和联系对数。
接下一行有N个整数,表示Wiskey联系第i个人的电话费用。
接着有M行,每行有两个整数X,Y,表示X能联系到Y,但是不表示Y也能联系X。
 

Output
输出最小联系人数和最小花费。
每个CASE输出答案一行。
 

Sample Input
12 16 2 2 2 2 2 2 2 2 2 2 2 2 1 3 3 2 2 1 3 4 2 4 3 5 5 4 4 6 6 4 7 4 7 12 7 8 8 7 8 9 10 9 11 10
 

Sample Output
3 6
 



#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long  
using namespace std;



/************************************************

Desiner:hl
time:2015/11/05
Exe.Time:374MS
Exe.Memory:2492K

题意:亲切的中文题。。
题解:tarjan 第一次使用。学了我好几天概念还有点模糊,求他入度为0的点
这些点是必须通知到的。且在联通块中找到每块联通块需要花费
代价最小的入度为0的点
修改了一下kuangbin神的版 orz 这样看起来方便处理 
************************************************/


const int MAXN = 50010;
const int MAXM = 100010;
struct Edge
{
    int to,next,st;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
int Index,top;
int scc;
bool Instack[MAXN];
int inde[MAXN];
void addedge(int u,int v)
{
    edge[tot].st=u;edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
}
void Tarjan(int u)  //kuangbin的tarjan模板 
{
    int v;
    DFN[u] = Low[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;
        }
        while( v != u );
    }
}
void solve(int n)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    Index = scc = top = 0;
    for(int i = 1;i <= n;i++)
        if(!DFN[i])
            Tarjan(i);
}
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}

int main(){
	int i,j,n,m,a,b;
	int cost[1111];
	while(~scanf("%d%d",&n,&m)){
		init();
		for(i=1;i<=n;i++){
			scanf("%d",&cost[i]);
		}
		for(i=1;i<=m;i++){
			scanf("%d %d",&a,&b);
			addedge(a,b);
		}
		solve(n);
		memset(inde,0,sizeof(inde));
		for(i=0;i<tot;i++){
			a=edge[i].st;
			b=edge[i].to;
			if(Belong[a]!=Belong[b]){
				inde[Belong[b]]++;
			}
		}
	//	for(i=1;i<=n;i++){
//			printf("%d  ",inde[i]);
//		}
		int ans1,ans2;
		ans1=ans2=0;
		int minans[MAXN];
		for(i=1;i<=scc;i++){
			if(inde[i]==0)
				ans1++;
				
			minans[i]=inf;
		}
		for(i=1;i<=n;i++){
			int tmp=Belong[i];
			if(inde[tmp]==0){
				minans[tmp]=min(minans[tmp],cost[i]);
			}
		}
		for(i=1;i<=scc;i++){
			if(minans[i]!=inf){
				ans2+=minans[i];
			}
		}
		//for(i=1;i<=n;i++){
//			printf("%d  ",Belong[i]);
//		}
//		
		printf("%d %d\n",ans1,ans2);
		
		
		
		
	}
	
	
}


发布了80 篇原创文章 · 获赞 0 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章