天天看點

HDOJ 3639 Summer Holiday

Summer Holiday

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2516    Accepted Submission(s): 1187

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​​

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
vector<int> map[1002];
stack<int> tarjan_stack;
int low[1002];
int dfn[1002];
bool vis[1002];
int belong[1002];
int in[1002];
int cost[1002];
int mincost[1002];
int cnt,pos;
void Init(int n)
{
  int i;
  cnt=pos=0;
  memset(low,0,sizeof(low));
  memset(dfn,0,sizeof(dfn));
  memset(vis,0,sizeof(vis));
  memset(belong,0,sizeof(belong));
  memset(in,0,sizeof(in));
  for(i=1; i<=n; i++)
    map[i].clear();
  for(i=1; i<=n; i++)
    mincost[i]=0x3f3f3f3f;
  while(!tarjan_stack.empty()) tarjan_stack.pop();
}
void Input(int n,int m)
{
  int i,a,b;
  for(i=1; i<=n; i++)
    scanf("%d",&cost[i]);
  for(i=1; i<=m; i++)
  {
    scanf("%d%d",&a,&b);
    map[a].push_back(b);
  }
}
void tarjan(int u)
{
  int i,t;
  dfn[u]=low[u]=++pos;
  vis[u]=true;
  tarjan_stack.push(u);
  for(i=0; i<map[u].size(); i++)
  {
    t=map[u][i];
    if(!dfn[t])
    {
      tarjan(t);
      if(low[t]<low[u])
        low[u]=low[t];
    }
    else if(vis[t] && low[u]>dfn[t])
      low[u]=dfn[t];
  }
  if(low[u]==dfn[u])
  {
    cnt++;
    while(!tarjan_stack.empty())
    {
        t=tarjan_stack.top();
        tarjan_stack.pop();
      vis[t]=false;
      belong[t]=cnt;
      if(t==u) break;
    }
  }
}
void Solve(int n,int m)
{
  int i,u,j,t;
  int number,total;
  Init(n);
  Input(n,m);
  for(i=1; i<=n; i++)
    if(!dfn[i])
      tarjan(i);
  for(u=1; u<=n; u++)
    for(j=0; j<map[u].size(); j++)
    {
      t=map[u][j];
      if(belong[u]!=belong[t])
        in[belong[t]]++;
    }
  for(i=1; i<=n; i++)
    mincost[belong[i]]=min(mincost[belong[i]], cost[i]);
  number=total=0;
  for(i=1; i<=cnt; i++)
    if(in[i]==0)
    {
      number++;
      total += mincost[i];
    }
  printf("%d %d\n",number,total);
}
int main()
{
  int N,M;
  while(cin>>N>>M)
    Solve(N,M);
  return 0;
}