问题 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;
}