hdu 1827(tarjan)

先用tarjan缩点, 然后入度为0的点就是必须要选择点同时也是最小的情况。

Summer Holiday

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


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
 

 

Author
威士忌
 

 

Source
 

 

Recommend
威士忌
 
 
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define N 1010
#define M 2020
int n,m;

struct node
{
    int from,to,next;
}edge[M];

int cos[N],low[N],vis[N],stack[N];
int nwcos[N];
int link[N];
int mark[N];
int cnt,pre[N];
int nn,top;
int d[N];


void add_edge(int u,int v)
{
    edge[cnt].from=u;
    edge[cnt].to=v;
    edge[cnt].next=pre[u];
    pre[u]=cnt++;
}


void dfs(int s,int k)
{
    low[s]=k;
    vis[s]=k;
    stack[++top]=s;
    mark[s]=1;
    for(int p=pre[s];p!=-1;p=edge[p].next)
    {
        int v=edge[p].to;
        if(mark[v]==0) dfs(v,k+1);
        if(mark[v]==1) low[s]=min(low[v],low[s]);
    }
    if(vis[s]==low[s])
    {
        nn++;
        int mi=INF;
        do
        {
            if(cos[ stack[top] ]<mi) mi=cos[ stack[top] ];
            link[ stack[top] ]=nn;
            mark[ stack[top] ]=-1;
        }while(stack[top--]!=s);
        nwcos[nn]=mi;
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        nn=0; cnt=0; top=0;
        memset(pre,-1,sizeof(pre));
        memset(mark,0,sizeof(mark));
        memset(d,0,sizeof(d));
        for(int i=1;i<=n;i++)
            scanf("%d",&cos[i]);
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_edge(x,y);
        }
        for(int i=1;i<=n;i++)
        {
            if(mark[i]==0) dfs(i,0);
        }
        for(int i=0;i<cnt;i++)
        {
            edge[i].from=link[edge[i].from];
            edge[i].to=link[edge[i].to];
            if(edge[i].to!=edge[i].from)
                d[edge[i].to]++;
        }
        ////////////////////////////////
        int num=0,ans=0;
        for(int i=1;i<=nn;i++)
        {
            if(d[i]==0)
            {
                num++;
                ans+=nwcos[i];
            }
        }
        printf("%d %d\n",num,ans);
    }
    return 0;
}

 

 

posted @ 2013-05-09 14:17  chenhuan001  阅读(207)  评论(0编辑  收藏  举报