还是畅通工程(最小生成树+kruskal+并查集)(Edmonds-Karp )

还是畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 83465    Accepted Submission(s): 37836


 

Problem Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
 

Output
对每个测试用例,在1行里输出最小的公路总长度。
 

Sample Input
 
 
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
 

Sample Output
 
 
3 5
Hint
Hint
Huge input, scanf is recommended.
 

Source
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 

//start
struct node {
    int u,v,w;
}e[100010];
int fa[100010];
int n,m;
bool cmp(node a,node b) //边的排序
{
    return a.w<b.w;
}
void init(){ //初始化
    for(int i=1;i<=m;i++){
        fa[i]=i;
    }
}
int find(int x){ //并查集
    if(x==fa[x]) return x;
    return fa[x]=find(fa[x]);
}
int kruskal(){
    init();
    int ans=0;
    sort(e+1,e+m+1,cmp);
    for(int i=1;i<=m;i++){
        int a=find(e[i].u);
        int b=find(e[i].v);
        if(a==b)  continue; //防止圈的出现
        fa[b]=a;
        ans+=e[i].w;
    }
    return ans;
}
int main(){
    cin>>n;
    m=(n-1)*n/2;
    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        e[i].u=a;
        e[i].v=b;
        e[i].w=c;
    }
    cout<<kruskal()<<endl;
}
//end

Edmonds-Karp算法实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 

//start
int n,m;
int g[10010][10010];
int pre[10010];
int bfs(int s,int t){
    int flow[10010];
    mms(pre,-1);
    flow[s]=INF; //初始化起点
    pre[s]=0;
    queue<int>q;
    q.push(s);
    while (!q.empty()) {
        int u=q.front();
        q.pop();
        if(u==t) break; //到达终点退出
        for(int i=1;i<=m;i++){ //BFS所有的点
            if(i!=s&&g[u][i]>0&&pre[i]==-1){
                pre[i]=u; //记录途径
                q.push(i);
                flow[i]=min(g[u][i],flow[u]); //更新结点流量
            }
        }
    }
    if(pre[t]==-1) return -1; //没有找到增广路
    return flow[t];//返回该路的流量
}
int maxflow(int s,int t){
    int MaxFlow=0;
    while (1) {
        int flow=bfs(s,t); //搜索最大流
        if(flow==-1) break; //没有找到新的增广路 退出
        int cur=t; //终点开始更新
        while (cur!=s) {
            int father=pre[cur]; //更新残留网络
            g[father][cur]-=flow; //正向+反向-
            g[cur][father]+=flow;
            cur=father;
        }
        MaxFlow+=flow;
    }
    return MaxFlow;
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int a,b,c;
        cin>>a>>b>>c;
        g[a][b]+=c; //可能有重边
    }
    cout<<maxflow(1,m);
}
//end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值