Edge Deletion(flody)

这篇博客讨论了如何在给定的简单连通无向图中,通过最少地删除边,保持任意两点之间的距离不变,同时确保剩余图仍为连通。题目涉及N个节点和M条边,以及每个边的长度,目标是找出可以删除的最大边数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题 N: Edge Deletion
时间限制: 2.000 Sec 内存限制: 1024 MB

题目描述
You are given a simple connected undirected graph with N vertices and M edges.
Edge i connects Vertex Ai and Vertex Bi, and has a length of Ci.Let us delete some number of edges while satisfying the conditions below. Find the maximum number of edges that can be deleted.
The graph is still connected after the deletion.
For every pair of vertices (s, t), the distance between s and t remains the same before and after the deletion.
Constraints
2 ≤ N ≤ 300
N-1 ≤ M ≤ N(N-1)/2
1 ≤ Ai < Bi ≤ N
1 ≤ Ci ≤ 109
(Ai, Bi) ≠ (Aj, Bj) if i ≠ j.
The given graph is connected.
All values in input are integers.

输入
Input is given from Standard Input in the following format:
N M
A1 B1 C1
A2 B2 C2
.
.
.
AM BM CM

输出
Print the answer.

样例输入 Copy
【输入样例1】
3 3
1 2 2
2 3 3
1 3 6

【输入样例2】
5 4
1 3 3
2 3 9
3 5 3
4 5 3

【输入样例3】
5 10
1 2 71
1 3 9
1 4 82
1 5 64
2 3 22
2 4 99
2 5 1
3 4 24
3 5 18
4 5 10

样例输出 Copy
【输出样例1】
1

【输出样例2】
0

【输出样例3】
5
提示
样例1解释
The distance between each pair of vertices before the deletion is as follows.

The distance between Vertex 1 and Vertex 2 is 2.
The distance between Vertex 1 and Vertex 3 is 5.
The distance between Vertex 2 and Vertex 3 is 3.
Deleting Edge 3 does not affect the distance between any pair of vertices. It is impossible to delete two or more edges while satisfying the condition, so the answer is 1.

样例2解释
No edge can be deleted.

代码:

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define rep(i,a,b) for(auto i=(a);i<=(b);++i)
#define dep(i,a,b) for(auto i=(a);i>=(b);--i)

using namespace std;
typedef long long ll;
const ll N = 3e2+10;
const ll inf = 1e9;
const ll mod = 998244353;
const double pi = acos(-1);

int n,m;
ll mp[N][N];
struct node{
    ll a,b,c;
}s[N*N];
int d[N][N],f[N][N];
void AC(){
    memset(mp,0x3f,sizeof mp);
    cin >> n >> m;
    rep(i,1,m){
        cin >> s[i].a >> s[i].b >> s[i].c;
        mp[s[i].a][s[i].b] = s[i].c;
        mp[s[i].b][s[i].a] = s[i].c;
        d[s[i].a][s[i].b] = 1;
        d[s[i].b][s[i].a] = 1;
    }
    rep(k,1,n){
        rep(i,1,n){
            rep(j,1,n){
                if(mp[i][j]>mp[i][k]+mp[j][k])mp[i][j] = mp[i][k]+mp[j][k];
            }
        }
    }
    int ans = 0;
    rep(k,1,n){
        rep(i,1,n){
            rep(j,1,n){
                if(mp[i][j]>=mp[i][k]+mp[j][k]&&d[i][j]){
                    d[i][j] = d[j][i] = 0;
                    ans++;
                }
            }
        }
    }

    cout << ans << endl;
}
/*

*/
int main(){
    int _ = 1;
    //scanf("%d",&_);
    while(_--){
        AC();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值