Problem E:reliable Nets
You ' re in charge of designing a campus network between buildings and is very worried about its
Reliability and its cost. So, you ' ve decided to build some redundancy into your network while keeping it
As inexpensive as possible. Speci?cally, you want-to-build the cheapest network so it if any one line
is broken, all buildings can still communicate. We ll call this a minimal reliable net.
Input
There'll is multiple test cases for this problem. Each test case would start with a pair of integers n
(≤15) and M (≤20) on a line indicating the number of buildings (numbered 1 through N) and the
Number of potential inter-building connections, respectively. (Values of n = m = 0 Indicate the end of
The problem.) The following m lines is of the form B1 B2 C (all positive integers) indicating that it costs
C to connect building B1 and B2. All connections is bidirectional.
Output
For each test case, you should print one line giving the cost of a minimal reliable net. If there is a
Minimal reliable net, the output line should is of the form:
The minimal cost to test case P is c.
where P is the number of the "Test Case" (starting at 1) and C are the cost. If There is no reliable net possible,
Output a line of the form:
There is no reliable net possible for test case p.
Sample Input
4 5
1 2 1
1 3 22015-08-19
2 4 2
3 4 1
2 3 1
2 1
1 2 5
0 0
Sample Output
The minimal cost to test Case 1 is 6.
There is no reliable net possible for test Case 2.
Test instructions
Give you a diagram to find a minimum right and pass all the points of the ring;
Exercises
Data Small direct DFS find a way to judge the update ans just fine
///by:1085422276#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<queue>#include<typeinfo>#include<map>#include<stack>typedef __int64 LL;#defineINF 0x7fffffffusing namespacestd;inline ll Read () {ll x=0, f=1; CharCh=GetChar (); while(ch<'0'|| Ch>'9') { if(ch=='-') f=-1; CH=GetChar (); } while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); } returnx*F;}//**************************************************************************************ll t,n,m,head[11111],vis[11111],vd[11111];ll ans,sum;structss{ll To,next; ll W;} e[300010];voidinit () {T=1; memset (Head,0,sizeof(head)); memset (Vis,0,sizeof(VIS)); memset (VD,0,sizeof(VD));}voidAdd (ll u,ll v,ll c) {e[t].to=v; E[T].W=C; E[t].next=Head[u]; Head[u]=t++;}voidBoo () { for(LL i=1; i<=n;i++)if(!vis[i])return; Ans=min (Sum,ans);}voidDFS (ll x) {if(x==1) {boo (); } for(LL i=head[x];i;i=E[i].next) { if(!Vd[i]) { if(i%2) vd[i+1]=1;Elsevd[i-1]=1; intbb=Vis[e[i].to]; Vis[e[i].to]=1; Vd[i]=1; Sum+=E[I].W; //printf ("%i64d---->%i64d\n", x,e[i].to);DFS (e[i].to); Sum-=E[I].W; Vis[e[i].to]=b; Vd[i]=0; if(i%2) vd[i+1]=0;Elsevd[i-1]=0; } }}intMain () {ll oo=1; while(SCANF ("%i64d%i64d", &n,&m)! =EOF) {ll a,b,c; if(n==0&&m==0) Break; Init (); for(LL i=1; i<=m;i++) {scanf ("%i64d%i64d%i64d",&a,&b,&c); //if (hash[a][b]) continue;Add (a,b,c); Add (B,A,C); } ans=inf; Sum=0; DFS (1LL); if(n==1|| n==2) ans=inf; if(m==1) ans=inf; if(ans==inf) {printf ("There is no reliable net possible for test case%i64d.\n", oo++); } Else{printf ("The minimal cost to test case%i64d is%i64d.\n", oo++, ans); } } return 0;}
Poj2735/gym 100650E Reliable Nets Dfs