@2017-2018 ACM-ICPC, Asia Daejeon Regional Contest H; How Many to Be Happy? ( 最小割 dinic算法)

本文主要是介绍@2017-2018 ACM-ICPC, Asia Daejeon Regional Contest H; How Many to Be Happy? ( 最小割 dinic算法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

O(n^2*m) 时间限制: 1 Sec  内存限制: 128 MB
提交: 80  解决: 32
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Let G be a connected simple undirected graph where each edge has an associated weight. Let’s consider the popular MST (Minimum Spanning Tree) problem. Today, we will see, for each edge e, how much modification on G is needed to make e part of an MST for G. For an edge e in G, there may already exist an MST for G that includes e. In that case, we say that e is happy in G and we define H(e) to be 0. However, it may happen that there is no MST for G that includes e. In such a case, we say that e is unhappy in G. We may remove a few of the edges in G to make a connected graph G′ in which e is happy. We define H(e) to be the minimum number of edges to remove from G such that e is happy in the resulting graph G′.

Figure E.1. A complete graph with 3 nodes.

Consider the graph in Figure E.1. There are 3 nodes and 3 edges connecting the nodes. One can easily see that the MST for this graph includes the 2 edges with weights 1 and 2, so the 2 edges are happy in the graph. How to make the edge with weight 3 happy? It is obvious that one can remove any one of the two happy edges to achieve that.
Given a connected simple undirected graph G, your task is to compute H(e) for each edge e in G and print the total sum.

 

输入

Your program is to read from standard input. The first line contains two positive integers n and m, respectively, representing the numbers of vertices and edges of the input graph, where n ≤ 100 and m ≤ 500. It is assumed that the graph G has n vertices that are indexed from 1 to n. It is followed by m lines, each contains 3 positive integers u, v, and w that represent an edge of the input graph between vertex u and vertex v with weight w. The weights are given as integers between 1 and 500, inclusive.

 

输出

Your program is to write to standard output. The only line should contain an integer S, which is the sum of H(e) where e ranges over all edges in G.

 

样例输入

3 3
1 2 1
3 1 2
3 2 3

 

样例输出

1

 

来源/分类

ICPC 2017 Daejeon 

 

题意:

N个点 M个边的无向图.  边权 不相同.,

求  成为最小生成树的每条边,   最少需要删去边的数量之和.

也就是说,  对每条边跑最小割. 

Dinc 复杂度O(n^2*m)   加上 每条边  ,  复杂度为: O(n^2*m^2)

 

 

[代码]

#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e5+10;
const int mod =1e9+7;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,m;struct node{int v,w,next; //u  v 从 u-v 权值为w
}edge[maxn];
struct Nn
{int u,v,w;
}a[maxn];
int head[maxn],num[maxn],start,END,cnt,sum;
void init()
{cnt=0;memset(head,-1,sizeof(head));memset(edge,0,sizeof(edge));
}void add(int u,int v,int w)
{edge[cnt].v=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].v=u;edge[cnt].w=w;edge[cnt].next=head[v];head[v]=cnt++;
}
int bfs()
{queue<int>Q;mem(num,0);num[start]=1;Q.push(start);while(!Q.empty()){int t=Q.front();Q.pop();if(t==END)return 1;for(int i=head[t];i!=-1;i=edge[i].next)// 链式前向星访问找增广路{int t1= edge[i].v;//下一个节点int t2= edge[i].w;// 当前点 权值if(t2&&num[t1]==0)// 当前点存在 并且下一个点没有访问{num[t1]=num[t]+1;// 点=1if(t1==END)//  结束return 1;Q.push(t1);}}}return 0;
}
int dfs(int u,int maxflow)
{if(u==END)return maxflow;int res=0;for(int i=head[u];i!=-1;i=edge[i].next){int t1=edge[i].v;// 下一个节点int t2=edge[i].w;// 当前节点if(t2&&num[t1]==num[u]+1){int temp=dfs(t1,min(maxflow-res,t2));// 选择流 小的一部分edge[i].w-=temp;// 正向减少edge[i^1].w+=temp;//反向增加res+=temp;if(res==maxflow)return res;}}if(!res)num[u]=-1;return res;
}
ll Dinic()
{ll ans=0;while(bfs()){ans+=dfs(start,INF);}return ans;
}int cmp(Nn a,Nn b)
{return a.w < b.w;
}
ll solve(int x)
{init();rep(i,1,m){if( a[i].w < a[x].w){add(a[i].u,a[i].v,1);}}start = a[x].u,END = a[x].v;return Dinic();
}
int main(int argc, char const *argv[])
{scanf("%d %d",&n,&m);rep(i,1,m){scanf("%d %d %d",&a[i].u,&a[i].v,&a[i].w);}sort(a+1,a+m+1,cmp);ll ans  = 0 ;rep(i,1,m)ans += solve(i);printf("%lld\n",ans );return 0;
}

 

这篇关于@2017-2018 ACM-ICPC, Asia Daejeon Regional Contest H; How Many to Be Happy? ( 最小割 dinic算法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565761

相关文章

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];