首页 > 代码库 > FOJ Problem 2271 X

FOJ Problem 2271 X

Problem 2271 X

Accept: 55    Submit: 200
Time Limit: 1500 mSec    Memory Limit : 32768 KB

 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 
 
题意:已经建立了一张图,希望尽可能多的去掉这张图上的一些边,并且使得任意两个城市之间的最短距离不变。
思路:可以先用floyd求任意两个城市之间的最短距离,存储于dp[i][j]中,原来的城市路径存储于d[i][j]中(若直接连边不存在d[i][j]=INF)
此时若dp[i][j]<d[i][j],说明d[i][j]可有可无,删掉即可,若dp[i][j]==d[i][j],那么有可能存在某个点k,让dp[i][k]+dp[k][j]==d[i][j],也就是说可以用其他更短的直接连边组合来代替原来的直接连边路径,那么这条d[i][j]也可以去掉。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define INF  0x3f3f3f3f
using namespace std;
const int N_MAX = 100 + 3;
int d[N_MAX][N_MAX];
int dp[N_MAX][N_MAX];
int V, M;//顶点数量,边数
void floyd() {
    for (int k = 0; k < V; k++) 
        for (int i = 0; i < V; i++) 
            for (int j = 0; j < V; j++) 
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
int main() {
    int T,cs=0;
    scanf("%d", &T);
    while (T--) {
        cs++;
        scanf("%d%d", &V, &M);
        memset(d, 0x3f, sizeof(d));
        for (int i = 0; i < V; i++) d[i][i] = 0;
        int res = 0;
        for (int i = 0; i < M; i++) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            a--, b--;
            if (d[a][b] == INF) { d[a][b] = c;}
            else {//!!一条路有多条边存在
                d[a][b] = min(d[a][b], c);
                res++;
                
            }
            d[b][a] = d[a][b];//!!!!!路径双向
        }
        
        memcpy(dp, d, sizeof(d));
        floyd();
        for (int i = 0; i < V; i++) {
            for (int j = i+1; j < V; j++) {//!!!!!
                
                if (d[i][j] == INF)continue;//两点没有直接连通路,不存在边不需要判断
                if (dp[i][j]<d[i][j]) {
                    res++;
                }
                else  {//相等,也可能i,j之间可以通过i->k->j的路走,这样就可以删掉直接连通路
                    for (int k = 0; k < V; k++) {
                        if (k == i || k == j)continue;//!!!!!
                        if (d[i][j] == dp[i][k] + dp[k][j]) {//!!!!!!
                            res++;
                            break;
                        }
                    }    
                }
            }
        }
        printf("Case %d: %d\n",cs,res);
    }
    return 0;
}

 

FOJ Problem 2271 X