您的位置:首页 > 其它

hdu 1827 summer holiday 强连通分量

2016-03-28 17:23 239 查看

Summer Holiday

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

Total Submission(s): 2454    Accepted Submission(s): 1156


[align=left]Problem Description[/align]
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计算出至少要通知多少人,至少得花多少电话费就能让所有人都被通知到吗?

 

[align=left]Input[/align]
多组测试数组,以EOF结束。

第一行两个整数N和M(1<=N<=1000, 1<=M<=2000),表示人数和联系对数。

接下一行有N个整数,表示Wiskey联系第i个人的电话费用。

接着有M行,每行有两个整数X,Y,表示X能联系到Y,但是不表示Y也能联系X。

 

[align=left]Output[/align]
输出最小联系人数和最小花费。

每个CASE输出答案一行。

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

3 6

 

[align=left]Author[/align]
威士忌
 

[align=left]Source[/align]
HDOJ 2007 Summer Exercise(3)- Hold by Wiskey

 

先求出强连通分量,然后把各个强连通分量缩成一个点判断这个点的度

当度为0的时候说明要通知,然后在求期间的费用

ACcode:

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 10000
using namespace std;
struct Edge{
int to,next;
}edge[maxn];
int head[maxn],tot;
int low[maxn],dfn[maxn],Stack[maxn],Belong[maxn];
int Index,top;
int scc;
bool Instack[maxn];
int num[maxn];
int cost[maxn];
int rdu[maxn];
void add(){
int u,v;
scanf("%d%d",&u,&v);
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void tarjan(int u){
int v;
low[u]=dfn[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;
num[scc]++;
}
while(v!=u);
}
}
void solve(int n){
for(int i=1;i<=n;++i)
if(!dfn[i])
tarjan(i);
int ans,mcost,v;
ans=mcost=0;
for(int i=1;i<=n;++i){
for(int k=head[i];k!=-1;k=edge[k].next){
v=edge[k].to;
if(Belong[i]!=Belong[v])
rdu[Belong[v]]++;
}
}
for(int i=1;i<=scc;++i)
if(!rdu[i]){
ans++;
int minn=9999999999;
for(int k=1;k<=n;++k)
if(Belong[k]==i)
minn=minn>cost[k]?cost[k]:minn;
// cout<<minn<<'\12';
mcost+=minn;
}
printf("%d %d\n",ans,mcost);
}
void init(){
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(Instack,0,sizeof(Instack));
memset(num,0,sizeof(num));
memset(rdu,0,sizeof(rdu));
memset(Belong,0,sizeof(Belong));
Index=scc=top=tot=0;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
init();
for(int i=1;i<=n;i++)scanf("%d",&cost[i]);
for(int i=0;i<m;i++)add();
solve(n);
}
return 0;
}
/*
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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: