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<stdio.h>
#include<string.h>
#include <iostream>
#include <bits/stdc++.h>
#define MAX 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll logo[1010];
ll map1[1010][1010];
ll dis[1010];
ll n,m,ans;
bool prim()
{
ll i,j,now;
ll sum=0;
for(i=1; i<=n; i++)
{
dis[i]=MAX;
logo[i]=0;
}
for(i=1; i<=n; i++)
{
dis[i]=map1[1][i];
}
dis[1]=0;
logo[1]=1;
for(i=1; i<n; i++)
{
now=MAX;
int min1=MAX;
for(j=1; j<=n; j++)
{
if(logo[j]==0&&dis[j]<min1)
{
now=j;
min1=dis[j];
}
}
if(now==MAX)
break;
logo[now]=1;
sum+=min1;
for(j=1; j<=n; j++)
{
if(logo[j]==0&&dis[j]>map1[now][j])
dis[j]=map1[now][j];
}
}
cout<<ans-sum<<endl;
}
int main()
{
ll T;
cin>>T;
while(T--)
{
cin>>n>>m;
ans=0;
memset(map1,0x3f3f3f3f,sizeof(map1));
for(int i=0; i<m; i++)
{
ll a,b,c;
cin>>a>>b>>c;
ans+=c;
if(c<map1[a+1][b+1])
map1[a+1][b+1]=map1[b+1][a+1]=c;
}
prim();
}
return 0;
}