最小树形图,測模版....

2248.   Channel Design


Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 2199   Accepted Runs: 740


We need irrigate our farms, but there is only one source of water nearby. So we need build some water channels with minimum cost.

In Figure (a), V1 indicates the source of water. Other N-1 nodes in the Figure indicate the farms we need to irrigate. An edge represents you can build a channel between the two nodes, to irrigate the target. The integers indicate the cost of a channel between two nodes.

Figure (b) represents a design of channels with minimum cost.

Input

There are multiple cases, the first line of each case contains two integers N and M (2 ≤ N ≤ 100; 1 ≤ M ≤ 10000), N shows the number of nodes. The following M lines, each line contains three integers i j cij, means we can build a channel from node Vi to node Vj, which cost cij. (1 ≤ ij ≤ Ni ≠ j; 1 ≤ cij ≤ 100)

The source of water is always V1.
The input is terminated by N = M = 0.

Output

For each case, output a single line contains an integer represents the minimum cost.

If no design can irrigate all the farms, output "impossible" instead.

Sample Input

5 8
1 2 3
1 3 5
2 4 2
3 1 5
3 2 5
3 4 4
3 5 7
5 4 3
3 3
1 2 3
1 3 5
3 2 1
0 0

Sample Output

17
6

Problem setter: Hill

Source: TJU Contest August 2006


Submit   List    Runs   Forum   Statistics

/* ***********************************************
Author        :CKboss
Created Time  :2015年07月04日 星期六 23时35分05秒
File Name     :TJU2248.cpp
************************************************ */#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>using namespace std;const int INF=0x3f3f3f3f;
const int maxn=110;int n,m;struct Edge
{int u,v,cost;
};Edge edge[maxn*maxn];
int pre[maxn],id[maxn],vis[maxn],in[maxn];int zhuliu(int root,int n,int m,Edge edge[])
{int res=0,u,v;while(true){for(int i=0;i<n;i++) in[i]=INF;for(int i=0;i<m;i++){if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v]){pre[edge[i].v]=edge[i].u;in[edge[i].v]=edge[i].cost;}}for(int i=0;i<n;i++)if(i!=root&&in[i]==INF) return -1;int tn=0;memset(id,-1,sizeof(id));memset(vis,-1,sizeof(vis));in[root]=0;for(int i=0;i<n;i++){res+=in[i];v=i;while(vis[v]!=i&&id[v]==-1&&v!=root){vis[v]=i; v=pre[v];}if(v!=root&&id[v]==-1){for(int u=pre[v];u!=v;u=pre[u])id[u]=tn;id[v]=tn++;}}if(tn==0) break;for(int i=0;i<n;i++)if(id[i]==-1) id[i]=tn++;for(int i=0;i<m;){v=edge[i].v;edge[i].u=id[edge[i].u];edge[i].v=id[edge[i].v];if(edge[i].u!=edge[i].v)edge[i++].cost-=in[v];elseswap(edge[i],edge[--m]);}n=tn;root=id[root];}return res;
}int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0) break;for(int i=0;i<m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w); u--; v--;edge[i].u=u; edge[i].v=v; edge[i].cost=w;}int ans=zhuliu(0,n,m,edge);if(ans==-1) puts("impossible");else printf("%d\n",ans);}return 0;
}
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5388191.html,如需转载请自行联系原作者 

TJU 2248. Channel Design 最小树形图相关推荐

  1. TJU 2248. Channel Design 最小树形图

    最小树形图,測模版.... 2248.   Channel Design Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 2199 ...

  2. 最小树形图及其生产方法

    诸位看官,这是我第一次在整篇文章的所有图片里面加水印.小弟写博客的时间不长,就有两篇博客被盗用并未注明原文网址.这一方面使我痛心不已,另一方面迫使我不得不重新考虑一下版权保护问题.小弟不是吝啬鬼,如果 ...

  3. poj3164(最小树形图模版)

    对最小树形图做个小小的总结: 1:清除自环,自环是不可能存在于任何最小树形图中的: 2:求出每个顶点的的最小入边: 3:判断该图是否存在最小树形图,由 1 可以判定,或者以图中顶点v作为根节点遍历该图 ...

  4. HDU6141(最小树形图)

    对最小树形图做个小小的总结: 1:清除自环,自环是不可能存在于任何最小树形图中的: 2:求出每个顶点的的最小入边: 3:判断该图是否存在最小树形图,由 1 可以判定,或者以图中顶点v作为根节点遍历该图 ...

  5. HDU4009(最小树形图)

    对最小树形图做个小小的总结: 1:清除自环,自环是不可能存在于任何最小树形图中的: 2:求出每个顶点的的最小入边: 3:判断该图是否存在最小树形图,由 1 可以判定,或者以图中顶点v作为根节点遍历该图 ...

  6. HDU2121(最小树形图的模版算法题)

    这个道题也是在看了大神之后敲的,我也是刚刚学习这个 ^ _ ^,看来离大佬们的距离还是太远了:以下内容也是在学习大佬讲解的内容之后,根据大佬们的详细讲解中总结出来的! 贪心算法.可以想到每次都找每个点 ...

  7. 模板 - 最小树形图(朱刘算法)

    整理的算法模板合集: ACM模板 目录 给定一个根的有向图的最小树形图 为给定根的树形图 判断无解的方法 给定一个根的有向图的最小树形图 给定包含 n 个结点,m 条有向边的一个图.试求一棵以结点 r ...

  8. 最小树形图复杂度分析

    如果实现得很聪明的话,可以达到找最小入边O(E),找环 O(V),收缩O(E),其中在找环O(V)这里需要一点技巧.这样每次收缩的复杂度是O(E),然后最多会收缩几次呢?由于我们一开始已经拿掉了所有的 ...

  9. hdu4966 最小树形图(最少辅导花费)

    题意:       以一些科目,和辅导班,每个科目最终要求修到某个等级,可以花一定的钱在辅导班把某一科目修到某一等级,进入辅导班的时候会有一个限制,那就是达到他给出的科目和等级限制,比如a b c d ...

最新文章

  1. linux 支持7代cpu型号,win7最高支持几代cpu
  2. 使用Bioconda管理生信软件(以bwa为例)
  3. tf.keras.activations.relu 激活函数 示例
  4. 在html中引用css样式表,怎么引用css样式?
  5. MATLAB与图像处理(四):将图片序列转化为视频文件,将视频文件转化为图片
  6. Asp.Net Core 项目搭建 基础配置 和MySql 的使用
  7. CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)
  8. 卡尔曼滤波对gps轨迹数据清洗_基于GPS的智能交通系统车辆定位精度提升技术
  9. redis设置密码和其它服务器连接
  10. linux 开机 找不到 文件系统 下载文件系统就好了,开机启动找不到文件系统的修复步骤...
  11. 机器学习实战:基于概率论的分类方法:朴素贝叶斯(源码解析,错误分析)...
  12. 微软宣布以197亿美元现金收购语音识别巨头Nuance
  13. 我从Web前端开发转到网页游戏开发
  14. FPGA实现千兆以太网发送
  15. WIN10去除磁盘写保护(只读属性)的步骤
  16. 中文汉字错别字纠错方法
  17. 代码审查工具FxCop建议采用的规则总结
  18. C#:Krypton控件使用方法详解(第一讲) —— kryptonButton
  19. 微软研究院科大实习生聚餐
  20. 非线性方程求根——牛顿迭代法

热门文章

  1. python程序员收入-python收入
  2. python培训深圳-深圳Python培训机构排名
  3. python基础代码库-python爬虫基础教程:requests库(二)代码实例
  4. 在python中、下列代码的输出是什么-python面试题详细总结(附答案)
  5. window中常用的命令
  6. es中的Plugin机制
  7. 基于Windows下使用Docker 部署Redis
  8. Golang 标准库提供的Log(一)
  9. MongoDB学习笔记——Master/Slave主从复制
  10. ORACLE 添加删除列脚本