X is a fully prosperous country, especially known for its complicated transportation networks. But recently, for the sake of better controlling by the government, the president Fat Brother thinks it’s time to close some roads in order to make the transportation system more effective.
Country X has N cities, the cities are connected by some undirected roads and it’s possible to travel from one city to any other city by these roads. Now the president Fat Brother wants to know that how many roads can be closed at most such that the distance between any two cities in country X does not change. Note that the distance between city A and city B is the minimum total length of the roads you need to travel from A to B.
The first line of the date is an integer T (1 <= T <= 50), which is the number of the text cases.
Then T cases follow, each case starts with two numbers N, M (1 <= N <= 100, 1 <= M <= 40000) which describe the number of the cities and the number of the roads in country X. Each case goes with M lines, each line consists of three integers x, y, s (1 <= x, y <= N, 1 <= s <= 10, x is not equal to y), which means that there is a road between city x and city y and the length of it is s. Note that there may be more than one roads between two cities.
For each case, output the case number first, then output the number of the roads that could be closed. This number should be as large as possible.
See the sample input and output for more details.
2 2 3 1 2 1 1 2 1 1 2 2 3 3 1 2 1 2 3 1 1 3 1Sample Output
Case 1: 2 Case 2: 0
题意:t组数据,每组数据 的第一行有 两个数n,m,n个点,m条边,下面m行,每行 x,y,z表示x到y 之间有一条通路;看看你能删除多少边使得 任意两点的最短路径不变;
思路: 想删这条边的话,一定存在一个它们两点之间的通路的路程,小于等于这条边,那么这条边才能删;
删这条边之间 一定要确定 当前边存在且没有被删过,
代码:
// 想删这条边的话,一定存在一个它们两点之间的通路的路程,小于等于这条边,那么这条边才能删;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 110
int use[Max][Max],G[Max][Max];
int book[Max][Max];
int n,m;
#define INF 0x3f3f3f3f
void init()
{
int i,j;
for(i = 0;i<=n;i++)
for(j= 0;j<=n;j++)
{
if(i==j) G[i][j] = 0;
else G[i][j] = INF;
}
memset(use,0,sizeof(use));
memset(book,0,sizeof(book));
}
int main()
{
int i,j,t,k;
scanf("%d",&t);
int tt = 1;
while(t--)
{
scanf("%d%d",&n,&m);
init();
int x,y,z;
int sum = 0;
for(i = 0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
if(use[x][y])
sum ++;
G[x][y] = min(G[x][y],z);
G[y][x] = G[x][y];
use[x][y] = 1; //G[x][y];
use[y][x] = 1; //G[x]
}
for(k = 1;k<=n;k++)
{
for(i = 1;i<=n;i++)
{
for(j = 1;j<=n;j++)
{
if(G[i][j]>=G[i][k]+G[k][j]&&i!=k&&k!=j)
{
G[i][j] = G[i][k]+G[k][j];
if(use[i][j]&&!book[i][j]) // 这条边存在且没有被删过;
{
book[i][j] = 1;
book[j][i] = 1;
sum++;
}
}
}
}
}
/*for(i = 1;i<=n;i++)
{
for(j = 1;j<=n;j++)
printf("%d ",G[i][j]);
printf("\n");
}*/
printf("Case %d: %d\n",tt++,sum);
}
return 0;
}
/*
3
4 5
1 2 2
2 3 2
1 4 1
2 4 1
4 3 1*/