首页 > 技术文章 > (最短路)第七届福建省大学生程序设计竞赛 Problem J- X

quintessence 2017-07-18 17:43 原文

 Problem Description

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.

 Input

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.

 Output

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.

 Sample Input

2 2 3 1 2 1 1 2 1 1 2 2 3 3 1 2 1 2 3 1 1 3 1

 Sample Output

Case 1: 2 Case 2: 0

 

用exist数组记录i,j之间是否有直接连接的边。用an表示最终答案,初始读入数据时,如果i\j之间已经存在边了,则an++(因为i\j间最终一定只保留一条边)。

除此之外,只需判断存在i\j之间存在的直达边能不能被其他的一系列边替代(这里只要距离<=直达边的距离就可以替代)。只需要在floyd过程中增加一个标记的数组来记录即可。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <list>
11 #include <vector>
12 #include <stack>
13 #define mp make_pair
14 //#define P make_pair
15 #define MIN(a,b) (a>b?b:a)
16 //#define MAX(a,b) (a>b?a:b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int MAX=1e2+5;
20 const int INF=1e8+5;
21 using namespace std;
22 //const int MOD=1e9+7;
23 typedef pair<ll,int> pii;
24 const double eps=0.000000001;
25 #define rank rankk
26 int t,num,cnt;
27 int n,m;
28 int d[MAX][MAX];
29 int ci[MAX][MAX];
30 bool exist[MAX][MAX],mark[MAX][MAX];
31 int V;//顶点数
32 void warshall_floyd()
33 {
34     for(int k=0;k<V;k++)
35         for(int i=0;i<V;i++)
36             for(int j=0;j<V;j++)
37                 {
38                     if(d[i][k]+d[k][j]<=d[i][j])
39                         mark[i][j]=true;
40                     d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
41                 }
42 }
43 int main()
44 {
45     scanf("%d",&t);
46     for(num=1;num<=t;num++)
47     {
48         memset(ci,0,sizeof(ci));
49         memset(d,0,sizeof(d));
50         memset(exist,false,sizeof(exist));
51         memset(mark,false,sizeof(mark));
52         cnt=0;
53         scanf("%d%d",&n,&m);
54         for(int i=0;i<n;i++)
55             for(int j=0;j<n;j++)
56                 d[i][j]=INF;
57         V=n;
58         for(int i=1;i<=m;i++)
59         {
60             int x,y,cost;
61             scanf("%d%d%d",&x,&y,&cost);
62             --x;--y;
63             exist[x][y]=exist[y][x]=true;
64             if(d[x][y]!=INF)
65                 ++cnt;
66             d[x][y]=min(d[x][y],cost);
67             d[y][x]=d[x][y];
68         }
69         warshall_floyd();
70         for(int i=0;i<n;i++)
71             for(int j=0;j<n;j++)
72             {
73                 if(exist[i][j])
74                 {
75                     exist[i][j]=exist[j][i]=false;
76                     if(mark[i][j])
77                         ++cnt;
78                 }
79             }
80         printf("Case %d: %d\n",num,cnt);
81     }
82     return 0;
83 }

 

推荐阅读