1. 程式人生 > >(最短路)第七屆福建省大學生程序設計競賽 Problem J- X

(最短路)第七屆福建省大學生程序設計競賽 Problem J- X

main tails return ros code and rect list def

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 }

(最短路)第七屆福建省大學生程序設計競賽 Problem J- X