Extended application of minimum spanning tree -------- secret milk transportation

                    农夫约翰要把他的牛奶运输到各个销售点。

During the transportation process, milk can be transported to some sales points, and then these sales points can be transported to other sales points respectively.
The smaller the total distance transported, the lower the cost of transportation.
Low-cost transportation is what Farmer John wants.
However, he does not want his competitors to know his specific transportation plan, so he hopes to adopt the second least expensive transportation plan instead of the smallest.
Now please help you find the transportation plan.
note::

If at least one side of the two schemes is different, we think it is a different scheme;
the scheme with the second smallest cost must be strictly smaller than the scheme with the smallest cost; the
answer is guaranteed to have a solution;

Input format The
first line is two integers N, MN, M, indicating the number of sales points and the number of traffic lines; the
next MM line 33 integers per line x, y, zx, y, z, indicating the point of sale xx and point of sale yy There is a line between them and the length is zz.
Output format
Output the total distance transported by the transportation scheme with the second smallest cost.
Data range
1≤N≤5001≤N≤500,

1≤M≤1041≤M≤104,

1≤z≤1091≤z≤109,

Data may contain multiple edges.
Input example:
4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample output:
450

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 510, M = 10010;
int n, m;
struct Edge{
 int a, b, w;
 bool f;
 bool operator < (const Edge &t) const{
          return w < t.w; 
 }
}edge[M];
int p[N];
int dist1[N][N], dist2[N][N];
int h[N], e[N * 2], w[N * 2], ne[N * 2], idx;
int find(int x){
 if (p[x] != x)   p[x] = find(p[x]);
 return p[x];
}
void add(int a, int b, int c){
 e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u, int fa, int maxd1, int maxd2, int d1[], int d2[]){
 d1[u] = maxd1, d2[u] = maxd2;
 for (int i = h[u]; ~i; i = ne[i]){
  int j = e[i];
  if (j != fa){
   int td1 = maxd1, td2 = maxd2;
   if (w[i] > td1)   td2 = td1, td1 = w[i];
   else   if (w[i] > td2 && w[i] < td1)   td2 = w[i];
   dfs(j, u, td1, td2, d1, d2);
  }
 }
}
int main(){
 scanf("%d%d", &n, &m);
 memset(h, -1, sizeof h);
  for (int i = 0; i < m; i ++){
  int a, b, w;
  scanf("%d%d%d", &a, &b, &w);
  edge[i] = {a, b, w};
 }
  sort(edge, edge + m);
  for (int i = 1; i <= n; i ++)   p[i] = i;
  LL sum = 0;
 for (int i = 0; i < m; i ++){
  int a = edge[i].a, b = edge[i].b, w = edge[i].w;
  int pa = find(a), pb = find(b);
  if (pa != pb){
   p[pa] = pb;
   sum += w;
   add(a, b, w),  add(b, a, w);
   edge[i].f = true;
  }
 }
  for (int i = 1; i <= n; i ++)  
       dfs(i, -1, -1e9, -1e9, dist1[i], dist2[i]);
          LL res = 1e18;
    for (int i = 0; i < m; i ++)
       if (!edge[i].f){
        int a = edge[i].a, b = edge[i].b, w = edge[i].w;
        LL t;
        if (w > dist1[a][b])
       t = sum + w - dist1[a][b];
  else  if (w > dist2[a][b])
       t = sum + w - dist2[a][b];
  res = min(res, t); 
   }
     printf("%lld\n", res);
        return 0;
}
164 original articles published · Like 112 · Visitors 6772

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/105466281