How Many to Be Happy?

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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.

样例输入

  1. 3 3
  2. 1 2 1
  3. 3 1 2
  4. 3 2 3

样例输出

  1. 1

来源/分类

ICPC 2017 Daejeon


最小生成树的MST性质的应用。我们想让某一条边一定是最小生成树中的边,只要找到任意一种点集的分配,使得这条边的两个顶点在不同的分配中且边权是连接这两个分配的所有边中最小的那一个。显然只有边权比它小的边才会影响它是不是在最小生成树中。于是我们可以只在图中保留边权小于当前边权的边,看看是否能找到一种点集的分配。显然当这个边的两个顶点在新图中仍然连通时,我们找不到这种分配,于是就需要砍掉若干边使两顶点不连通,于是题目就转化为了最小割问题。
  1. #include<bits/stdc++.h>
  2. #define INF LLONG_MAX/2
  3. #define N 505
  4. using namespace std;
  5.  
  6. struct ss
  7. {
  8. int v,next;
  9. long long flow;
  10. };
  11. int head[N],now_edge=,S,T;
  12. ss edg[N*];
  13.  
  14. void init()
  15. {
  16. now_edge=;
  17. memset(head,-,sizeof(head));
  18. }
  19.  
  20. void addedge(int u,int v,long long flow)
  21. {
  22. edg[now_edge]=(ss){v,head[u],flow};
  23. head[u]=now_edge++;
  24. edg[now_edge]=(ss){u,head[v],flow};
  25. head[v]=now_edge++;
  26. }
  27.  
  28. int dis[N];
  29.  
  30. int bfs()
  31. {
  32. memset(dis,,sizeof(dis));
  33. queue<int>q;
  34. q.push(S);
  35. dis[S]=;
  36.  
  37. while(!q.empty())
  38. {
  39. int now=q.front();
  40. q.pop();
  41.  
  42. for(int i=head[now];i!=-;i=edg[i].next)
  43. {
  44. ss &e=edg[i];
  45. if(e.flow>&&dis[e.v]==)
  46. {
  47. dis[e.v]=dis[now]+;
  48. q.push(e.v);
  49. }
  50. }
  51. }
  52.  
  53. if(dis[T]==)return ;
  54. return ;
  55. }
  56.  
  57. int current[N];
  58. long long dfs(int x,long long maxflow)
  59. {
  60. if(x==T)return maxflow;
  61. for(int i=current[x];i!=-;i=edg[i].next)
  62. {
  63. current[x]=i;
  64.  
  65. ss &e=edg[i];
  66. if(e.flow>&&dis[e.v]==dis[x]+)
  67. {
  68. long long flow=dfs(e.v,min(maxflow,e.flow));
  69.  
  70. if(flow!=)
  71. {
  72. e.flow-=flow;
  73. edg[i^].flow+=flow;
  74. return flow;
  75. }
  76. }
  77. }
  78. return ;
  79. }
  80.  
  81. long long dinic()
  82. {
  83. long long ans=,flow;
  84.  
  85. while(bfs())
  86. {
  87. for(int i=;i<N;i++)current[i]=head[i];
  88. while(flow=dfs(S,INF))ans+=flow;
  89. }
  90. return ans;
  91. }
  92.  
  93. int from[N],to[N],w[N];
  94.  
  95. int main()
  96. {
  97. int n,m;
  98. scanf("%d %d",&n,&m);
  99. for(int i=;i<=m;i++)
  100. {
  101. scanf("%d %d %d",&from[i],&to[i],&w[i]);
  102. }
  103.  
  104. int ans=;
  105. for(int i=;i<=m;i++)
  106. {
  107. init();
  108. for(int j=;j<=m;j++)
  109. if(w[j]<w[i])addedge(from[j],to[j],);
  110.  
  111. S=from[i];
  112. T=to[i];
  113. ans+=dinic();
  114. }
  115. printf("%d\n",ans);
  116. return ;
  117. }

随机推荐

  1. 1143: [CTSC2008]祭祀river

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4018  Solved: 2048[Submit][Status][Discuss] Descript ...

  2. iView - Form中想要重置DatePicker生效,必须给DatePicker绑定value属性

    Form中想要重置DatePicker生效,必须给DatePicker绑定value属性

  3. mysql零散操作

    添加对外用户 CREATE USER 'admin'@'%' IDENTIFIED BY '!QAZ2wsx'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%'; ...

  4. hprose 1.0(rpc 框架) - 内部数据标准

    hprose 1.0 内部数据标准 方法的描述  {    // 请求调用格式    'C'.writeString('method1').'a'.count($params).'{'.'m'.cou ...

  5. python3爬虫之Urllib库(二)

    在上一篇文章中,我们大概讲了一下urllib库中最重要的两个请求方法:urlopen()  和  Request() 但是仅仅凭借那两个方法无法执行一些更高级的请求,如Cookies处理,代理设置等等 ...

  6. 721. Accounts Merge

    https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...

  7. winform中使用webBrowser时如何与JS交互

    最近写一个GEPlugin项目,要用到geWebBrowser与JS进行交互. 这个geWebBrowser的事件 private void geWebBrowser1_DocumentComplet ...

  8. tomcat8+idea远程调试

    window下 setenv.bat增加 set JPDA_OPTS=-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n lin ...

  9. spoj 104 Highways(Matrix-tree定理)

    spoj 104 Highways 生成树计数,matrix-tree定理的应用. Matrix-tree定理: D为无向图G的度数矩阵(D[i][i]是i的度数,其他的为0),A为G的邻接矩阵(若u ...

  10. P2615 神奇的幻方

    P2615 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首 ...