传送门
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
题解
将联系网络缩点,维护每个缩点中的最小值,累和,水。
CODE:
#include<cstdio>
#include<cstring>
const int N=1005;
struct edge
{
int nxt,to;
}a[N<<1];
int head[N];
int dfn[N],low[N],block[N],mincost[N];
int in[N];
int s[N],top;
bool instack[N];
int cost[N];
int n,m,x,y,num,tot,Time,Ans;
long long ans;
inline int min(const int &a,const int &b){return a<b?a:b;}
inline void add(int x,int y)
{
a[++num].nxt=head[x],a[num].to=y,head[x]=num;
}
void dfs(int now)
{
dfn[now]=low[now]=++Time;
instack[now]=1;
s[++top]=now;
for(int i=head[now];i;i=a[i].nxt)
if(!dfn[a[i].to])
{
dfs(a[i].to);
low[now]=min(low[now],low[a[i].to]);
}
else if(instack[a[i].to]) low[now]=min(low[now],dfn[a[i].to]);
if(low[now]==dfn[now])
{
int tmp;
mincost[++tot]=1e9;
do
{
tmp=s[top--];
block[tmp]=tot;
instack[tmp]=0;
mincost[tot]=min(mincost[tot],cost[tmp]);
}while(tmp!=now);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,0,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(in,0,sizeof(in));
num=tot=Time=ans=Ans=0;
for(int i=1;i<=n;i++)
scanf("%d",&cost[i]);
for(int i=1;i<=m;i++)
scanf("%d%d",&x,&y),add(x,y);
for(int i=1;i<=n;i++)
if(!dfn[i]) dfs(i);
for(int j=1;j<=n;j++)
for(int i=head[j];i;i=a[i].nxt)
if(block[j]!=block[a[i].to]) in[block[a[i].to]]++;
for(int i=1;i<=tot;i++)
if(!in[i]) ans+=mincost[i],Ans++;
printf("%d %lld\n",Ans,ans);
}
return 0;
}