FZU 2271 floyd

FZU-2271

题意:
无向图,无自环,问最多可以删除多少条边,使得任意两点间的最短路长度都不变。
tags:
floyd 判一下,如果两点(u,v)之间可通过一点 k 松驰,那就记录 (u,v) 有中间点 k ,那么边 (u,v) 就不可取。

//https://vjudge.net/problem/FZU-2271#author=Purity
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<bitset>
#include<vector>
#include<set>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 105;

int n, m, dis[N][N], path[N][N];
void Init() {
    mes(dis, INF); rep(i,0,N-1) dis[i][i]=0;
    mes(path, -1);
}
int main()
{
    int T;  scanf("%d", &T);
    rep(cas, 1, T)
    {
        Init();
        scanf("%d%d", &n, &m);
        int x, y, z;
        rep(i,1,m)
        {
            scanf("%d%d%d", &x, &y, &z);
            dis[x][y] = dis[y][x] = min(dis[x][y], z);
            path[x][y] = path[y][x] = y;
        }
        rep(k,1,n)
            rep(i,1,n) rep(j,1,n)
                if(k!=i && k!=j && i!=j) {
                    if(dis[i][j] >= dis[i][k]+dis[k][j]) {
                        dis[i][j] = dis[i][k]+dis[k][j];
                        path[i][j] = k;
                    }
                }
        int ans = 0;
        rep(i,1,n) rep(j,i+1,n)
            if(path[i][j]==i || path[i][j]==j)
                ++ans;
        printf("Case %d: ", cas);
        printf("%d\n", m-ans);
    }

    return 0;
}
posted @ 2018-05-10 15:12  v9fly  阅读(111)  评论(0编辑  收藏  举报