Test instructions: Gives a set of non-direction graphs with m points, and then gives N points, which are connected to a point in the graph with one edge respectively. For each query, find the minimum cost of 3-point connectivity. It is possible that 3 points are not interoperable. , the minimum price is the sum of the red side's weights.
Idea: First of the M-point of the non-direction of the diagram to find the shortest path between 22, with Floyd. Next, for each query, the poor lift m points, the sum of 3 points to the point of the distance, the minimum can be obtained.
1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5 #defineINB 0x7f7f7f7f6 using namespacestd;7 Const intn=10005;8 intTsf[n];//Telephone I received the transit station9 intgph[505][505];//adjacency matrix representation diagramTen One intnexte[505][505];//auxiliary matrices for Floyd A intdis[505][505]; - - the intCalintAintBintCintm) - { - intSmall=INB; - for(intk=1; k<=m; k++) + { - if(dis[a][k]==inb| | dis[b][k]==inb| | dis[c][k]==INB) + Continue; A if(dis[a][k]+dis[b][k]+dis[c][k]<small) atsmall=dis[a][k]+dis[b][k]+Dis[c][k]; - } - if(SMALL>=INB)return 0; - returnSmall; - } - in voidFloydintm) - { tomemset (Nexte,0x80,sizeof(Nexte));//Initialize to negative +memset (DIS,0x7f,sizeof(dis));//Initialize to Infinity - the //initialize next for reachable * for(intI=1; i<=m; i++) $ {Panax Notoginseng for(intj=1; j<=m; J + +) - { the if(I==J) dis[i][i]=0;//to their + if(gph[i][j]>0)//If I to J have side A { theNexte[i][j]=j;//this is the path matrix +DIS[I][J]=GPH[I][J];//graph without Direction - } $ } $ } - - for(intk=1; k<=m; k++) the { - for(intI=1; i<=m; i++)Wuyi { the for(intj=1; j<=m; J + +) - { Wu - if(Dis[i][k]>=inb | | dis[k][j]>=inb)Continue;//paragraph two, one of which is already unreachable . About if(dis[i][j]>dis[i][k]+ Dis[k][j])//This is a shorter road . $ { -dis[i][j]=dis[i][k]+Dis[k][j]; -NEXTE[I][J]=NEXTE[I][K];//Modify Path - } A } + } the } - } $ the intMain () the { the //freopen ("E://input.txt "," R ", stdin); the intN, M, L, j=0; - intA, B, V, q; in while(cin>>n>>m>>l) the { thememset (GPH,0,sizeof(GPH));//Initialize About for(intI=0; i<n; i++)//Enter the broker to which the phone is connected thescanf"%d", &tsf[i+1]); the for(intI=0; i<l; i++)//Enter a broker map the { +scanf"%d%d%d",&a,&b,&v); -gph[a][b]=v; thegph[b][a]=v;Bayi } theFloyd (M);//Freud start thescanf"%d",&q); -printf"Case #%d\n",++j); - for(intI=0; i<q; i++)//Process each query the { thescanf"%d%d%d",&a,&b,&v); the intcn1=cal (tsf[a],tsf[b],tsf[v],m); the if(ANS) -printf"Line %d:the minimum cost for this line is%d.\n", i+1, ans); the Else theprintf"Line %d:impossible to connect!\n", i+1); the }94 } the return 0; the}
AC Code
ZOJ 3396 Conference Call (3-point minimum spanning tree)