Networking
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 6589 |
|
Accepted: 3590 |
Description You is assigned to the design network connections between certain points in a wide area. You is given a set of points in the area, and a set of possible routes for the cables, which may connect pairs of points. For each possible route between and points, you were given the length of the cable that's needed to connect the points ove R that route. Note that there may exist many possible routes between, given points. It is assumed, the given possible routes connect (directly or indirectly) each of the points in the area. Your task is to design the network for the area, so there is a connection (direct or indirect) between every TS (i.e., all the points is interconnected, but not necessarily by a direct cable), and which the total length of the used Cable is minimal.Input The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains integers:the first defines the number P of the given points, and the second the Nu Mber R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers:the first and numbers Identify the points, and the third gives the length of the route. The numbers is separated with white spaces. A data set giving only one number p=0 denotes the end of the input. The data sets is separated with a empty line. The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes is identified with integers between 1 and P (inclusive). The routes between II points I and J may be given as I J or as J I.
Output For each data set, print one number in a separate line that gives the total length of the cable used for the entire design Ed Network.Sample Input 1 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120
Sample Output 0171626
Source Southeastern Europe 2002 |
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 10010#define Mod 10000007#define Lson l,mid,idx<<1# Define Rson mid+1,r,idx<<1|1#define LC idx<<1#define RC Idx<<1|1const Double EPS = 1e-11;const double PI = ACOs ( -1.0);//const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace Std;int n,m;i NT Mp[n][n];int par[n],rank[n];struct Edge {int u,v,cost;}; BOOL CMP (const edge& E1,CONST edge& E2) {return e1.cost<e2.cost;} Edge es[n];int e;void init (int N) {for (int i=0; i<=n; i++) {par[i]=i; rank[i]=0; }}int finds (int x) {if (par[x]==x) return x; Return Par[x]=finds (Par[x]);} void Unite (int x,int y) {x=finds (x); Y=finds (y); if (x==y) return; if (Rank[x]<rank[y]) Par[x]=y; else {par[y]=x; if (Rank[x]==rank[y]) rank[x]++; }}bool Same (int x,int y) {return finds (x) ==finds (y);} int Kruskal () {sort (es,es+e,cmp); Init (E); int res=0; for (int i=0; i<e; i++) {edge e=es[i]; if (!same (E.U,E.V)) {res+=e.cost; Unite (E.U,E.V); }} return res;} int main () {while (cin>>n&&n) {e=0; cin>>m; for (int i=0, i<=n; i++) for (int j=0; j<=n; J + +) {//if (I==J) mp[i][j]=0;//E LSE Mp[i][j]=inf; } int x,y,cost; for (int i=1; i<=m; i++) {scanf ("%d%d%d", &x,&y,&cost); if (mp[x][y]>cost) mp[x][y]=cost; } for (int i=1, i<=n; i++) for (int j=1; j<=n; J + +) {if (i==j| | Mp[i][j]==inf) continue; Es[e].u=i; Es[e].v=j; Es[e++].cost=mP[I][J]; } int Ans=kruskal (); cout<<ans<<endl; } return 0;}
POJ 1287 Networking (minimum spanning tree)