TJU 2248. Channel Design 最小树形图
最小树形图,測模版....
2248. Channel Design
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 2199 Accepted Runs: 740
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 ≤ i, j ≤ N; i ≠ 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;
}
TJU 2248. Channel Design 最小树形图相关推荐
- TJU 2248. Channel Design 最小树形图
最小树形图,測模版.... 2248. Channel Design Time Limit: 1.0 Seconds Memory Limit: 65536K Total Runs: 2199 ...
- 最小树形图及其生产方法
诸位看官,这是我第一次在整篇文章的所有图片里面加水印.小弟写博客的时间不长,就有两篇博客被盗用并未注明原文网址.这一方面使我痛心不已,另一方面迫使我不得不重新考虑一下版权保护问题.小弟不是吝啬鬼,如果 ...
- poj3164(最小树形图模版)
对最小树形图做个小小的总结: 1:清除自环,自环是不可能存在于任何最小树形图中的: 2:求出每个顶点的的最小入边: 3:判断该图是否存在最小树形图,由 1 可以判定,或者以图中顶点v作为根节点遍历该图 ...
- HDU6141(最小树形图)
对最小树形图做个小小的总结: 1:清除自环,自环是不可能存在于任何最小树形图中的: 2:求出每个顶点的的最小入边: 3:判断该图是否存在最小树形图,由 1 可以判定,或者以图中顶点v作为根节点遍历该图 ...
- HDU4009(最小树形图)
对最小树形图做个小小的总结: 1:清除自环,自环是不可能存在于任何最小树形图中的: 2:求出每个顶点的的最小入边: 3:判断该图是否存在最小树形图,由 1 可以判定,或者以图中顶点v作为根节点遍历该图 ...
- HDU2121(最小树形图的模版算法题)
这个道题也是在看了大神之后敲的,我也是刚刚学习这个 ^ _ ^,看来离大佬们的距离还是太远了:以下内容也是在学习大佬讲解的内容之后,根据大佬们的详细讲解中总结出来的! 贪心算法.可以想到每次都找每个点 ...
- 模板 - 最小树形图(朱刘算法)
整理的算法模板合集: ACM模板 目录 给定一个根的有向图的最小树形图 为给定根的树形图 判断无解的方法 给定一个根的有向图的最小树形图 给定包含 n 个结点,m 条有向边的一个图.试求一棵以结点 r ...
- 最小树形图复杂度分析
如果实现得很聪明的话,可以达到找最小入边O(E),找环 O(V),收缩O(E),其中在找环O(V)这里需要一点技巧.这样每次收缩的复杂度是O(E),然后最多会收缩几次呢?由于我们一开始已经拿掉了所有的 ...
- hdu4966 最小树形图(最少辅导花费)
题意: 以一些科目,和辅导班,每个科目最终要求修到某个等级,可以花一定的钱在辅导班把某一科目修到某一等级,进入辅导班的时候会有一个限制,那就是达到他给出的科目和等级限制,比如a b c d ...
最新文章
- linux 支持7代cpu型号,win7最高支持几代cpu
- 使用Bioconda管理生信软件(以bwa为例)
- tf.keras.activations.relu 激活函数 示例
- 在html中引用css样式表,怎么引用css样式?
- MATLAB与图像处理(四):将图片序列转化为视频文件,将视频文件转化为图片
- Asp.Net Core 项目搭建 基础配置 和MySql 的使用
- CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)
- 卡尔曼滤波对gps轨迹数据清洗_基于GPS的智能交通系统车辆定位精度提升技术
- redis设置密码和其它服务器连接
- linux 开机 找不到 文件系统 下载文件系统就好了,开机启动找不到文件系统的修复步骤...
- 机器学习实战:基于概率论的分类方法:朴素贝叶斯(源码解析,错误分析)...
- 微软宣布以197亿美元现金收购语音识别巨头Nuance
- 我从Web前端开发转到网页游戏开发
- FPGA实现千兆以太网发送
- WIN10去除磁盘写保护(只读属性)的步骤
- 中文汉字错别字纠错方法
- 代码审查工具FxCop建议采用的规则总结
- C#:Krypton控件使用方法详解(第一讲) —— kryptonButton
- 微软研究院科大实习生聚餐
- 非线性方程求根——牛顿迭代法