hdu1827——Summer Holiday

Summer Holiday

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


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
HDOJ 2007 Summer Exercise(3)- Hold by Wiskey
 

Recommend
威士忌   |   We have carefully selected several similar problems for you:   1823  1824  1826  1822  1825

就那么回事,tarjan然后缩点,然后找入度为0的点,暴力找出这些个强连通分量里面最便宜的那个点


#include<map>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

const int N  = 1010;
const int M  = 2010;
const int inf = 0x3f3f3f3f;
int DFN[N];
int low[N];
int block[N];
int Stack[N];
int in[N];
bool instack[N];
int head[N];
int cost[N];
int min_price[N];
int tot, sccnum, index, top, n, m;

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

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

void tarjan(int u)
{
    DFN[u] = low[u] = ++index;
    Stack[top++] = u;
    instack[u] = 1;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if (DFN[v] == 0)
        {
            tarjan(v);
            if (low[u] > low[v])
            {
                low[u] = low[v];
            }
        }
        else if (instack[v])
        {
            if (low[u] > DFN[v])
            {
                low[u] = DFN[v];
            }
        }
    }
    if (DFN[u] == low[u])
    {
        sccnum++;
        do
        {
            top--;
            block[Stack[top]] = sccnum;
            instack[Stack[top]] = 0;
        }while ( top >=0 && Stack[top] != u);
    }
}

void solve()
{
    memset( instack, 0, sizeof(instack) );
    memset( DFN, 0, sizeof(DFN) );
    memset( low, 0, sizeof(low) );
    memset( in, 0, sizeof(in) );
    memset( min_price, inf, sizeof(min_price) );
    sccnum = index = top = 0;
    for (int i = 1; i <= n; i++)
    {
        if (DFN[i] == 0)
        {
            tarjan(i);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = head[i]; j != -1; j = edge[j].next)
        {
            if(block[i] != block[edge[j].to])
            {
                in[block[edge[j].to]]++;
            }
        }
    }
    int res = 0;
    int tmp, ans = 0;
    for(int i = 1; i <= sccnum; i++)
    {
        if(in[i] == 0)
        {
            res++;
            tmp = inf;
            for(int j = 1; j <= n; j++)
            {
                if(block[j] == i)
                {
                    tmp = min(tmp, cost[j]);
                }
            }
            ans += tmp;
        }
    }
    printf("%d %d\n", res, ans);
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        memset( head, -1, sizeof(head) );
        tot = 0;
        int u, v;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &cost[i]);
        }
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &u, &v);
            addedge(u, v);
        }
        solve();

    }
    return 0;
}



你可能感兴趣的:(hdu1827——Summer Holiday)