Description
ACM Bus company runs a large bus network. To go from city A to city B, there could be a express
route with no stops between the two cities. There could also be local routes with stops at different
cities prior to city B. However, ever since the ICPC high speed rail went into operation, ACM Bus
company has been steadily losing customers. It is no longer financially possible to maintain so many
bus routes. The annual cost of maintaining 1 kilometer of bus route is $1. The ACM board has decided
to streamline all the routes serviced by the company in order to lower the total maintenance cost. In
consequence, many of the routes (whether be direct or indirect routes) are to be eliminated. The ACM
board proposed three simple rules in eliminating the routes.
1. All cities must still be served.
2. There will be no multiple services/route to the same city.
3. The total routes eliminated in terms of kilometres must be as large as possible.
Given the bus route map, forecast the annual total saving in maintenance cost if the board’s plan is
put into practice.
Technical Specification
• The number of cities served is c, 2 ≤ c ≤ 1, 000. The cities are numbered from 0 to c − 1.
• The number of express routes is r, 1 ≤ r ≤ c(c − 1)/2. The distance of any express route is
between 1 and 1000 kilometers.
Input
First line of input is an integer n, n ≤ 15, indicating number of test cases to follow. For each test case,
the first line contains two integers c and r, indicating the number of cities and the number of express
routes, respectively. The next r lines each contains three integers c1 , c2, and dist, indicating that there
is an express route between cities c1 and c2 and the distance between the two cities is dist kilometers.
Output
For each test case, output the projected saving if the ACM Board’s plan is put into practice.
Sample Input
2
3 3
0 1 3
2 1 1
2 0 2
4 5
3 2 4
1 0 4
2 1 1
0 2 2
1 3 2
Sample Output
3
8
题解:
最小生成树模板题。
代码如下:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 2017
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1001113
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;
int n, m;
ll sum;
struct node
{
int start,end,power;
} edge[1000050];
int pre[5050];
int cmp(node a, node b)
{
return a.power<b.power;
}
int find(int x)
{
if(x!=pre[x])
{
pre[x]=find(pre[x]);
}
return pre[x];
}
void merge(int x,int y,int n)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
sum+=edge[n].power;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
sum=0;
int i;
int start,end,power;
ll s=0;
for(i=0; i<m; i++)
{
scanf("%d %d %d", &start, &end, &power);
edge[i].start=start,edge[i].end=end,edge[i].power=power;
s+=power;
}
for(i=0; i<m; i++)
{
pre[i]=i;
}
sort(edge, edge+m,cmp);
for(i=0; i <m; i++)
{
merge(edge[i].start,edge[i].end,i);
}
cout<<s-sum<<endl;
}
return 0;
}