Summer Holiday
Time limit:10000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2054 Accepted Submission (s): 941
Problem Descriptionto 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
I heard that LCY to help everyone booked a new Malaysian Thai 7th tour, Wiskey is really happy night can not sleep ah, he thought to be quick to tell everyone, although he has all the contact information, but a link in the past is too time and telephone costs. He knows that others have other contacts, so that he can notify others and let others help inform others. Can you help wiskey figure out at least how many people to tell, at least how much will it cost to get everyone notified?
Input multiple set of test arrays, ending with EOF.
The first line is two integers N and M (1<=n<=1000, 1<=m<=2000), representing the number of people and the contact logarithm.
The next line has n integers, indicating the phone charges for wiskey contact person I.
Then there are m lines, each line has two integer x, y, which means x can contact Y, but does not mean Y can also contact X.
The output outputs the minimum number of contacts and the minimum cost.
Each case outputs an answer line.
Sample Input
12 162 2 2 2 2 2 2 2 2 2 2 2 1 33 22 13 42 43 55 44 66 47 47 127 88 78 910 911 10
Sample Output
3 6
Author whisky
Test instructions do not say, first Tarjan, and then find out the penetration of each unicom block, into the degree of 0 to give at least one of the Unicom block to call a person, the cost of the link to take the least cost of the one.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MOD 1000000009#define INF 0x3f3f3f3f#define Pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ( "%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, & c) #define PF Printf#define DBG pf ("hi\n") typedef long long ll;using namespace Std;const int maxn = 20010; Point const int MAXM = 500010;//number of sides struct edge{int tO,next;} The value of the Edge[maxm];int head[maxn],tot;int low[maxn],dfn[maxn],stack[maxn],belong[maxn];//belong array is 1~sccint Index,top; int scc;//The number of strong Unicom components bool Instack[maxn];int num[maxn];//Each strong Unicom component contains the number of points, the array number is 1~scc//num array is not necessarily required, the actual situation void Addedge (int u , int 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) {memset (dfn,0,sizeof (DFN)); memset (instack,false,sizeof (instack)); memset (num,0,sizeof (num)); Index=scc=top=0; for (int i=1;i<=n;i++)//point number starting with 1 if (! Dfn[i]) Tarjan (i);} int n,m;int cost[maxn],d[maxn];void init () {tot=0; memset (head,-1,sizeof (head)); Memset (d,0,sizeof (d)); int main () {#ifndef Online_judge freopen ("C:/users/asus1/desktop/in.txt", "R", stdin), #endif int i,j,u,v; while (~SFF (n,m)) {init (); for (i=1;i<=n;i++) SF (Cost[i]); for (i=0;i<m;i++) {SFF (u,v); Addedge (U,V); } solve (n); for (u=1;u<=n;u++) {for (i=head[u];~i;i=edge[i].next) {int v=edge[i].to; if (Belong[u]!=belong[v]) d[belong[v]]++; }} int num=0,ans=0; for (i=1;i<=scc;i++) {if (d[i]==0) {int min=inf; for (j=1;j<=n;j++) {if (Belong[j]==i&&min>cost[j]) Min=cOST[J]; } ans+=min; num++; }} pf ("%d%d\n", Num,ans); } return 0;}
Sourcehdoj Summer Exercise (3)-Hold by Wiskey
Recommend Whisky | We have carefully selected several similar problems for you:1823 1824 1826 1269 1822
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Summer Holiday (hdu 1827 strong unicom)