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
pid=2248">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];
else
swap(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 最小树形图的更多相关文章
- bzoj4349: 最小树形图
最小树形图模板题…… 这种\(O(nm)\)的东西真的能考到么…… #include <bits/stdc++.h> #define N 60 #define INF 1000000000 ...
- hdu 4966 GGS-DDU (最小树形图)
比较好的讲解:http://blog.csdn.net/wsniyufang/article/details/6747392 view code//首先为除根之外的每个点选定一条入边,这条入边一定要是 ...
- HDU 4966 GGS-DDU(最小树形图)
n个技能,每个技能有0-a[i]的等级,m个课程,每个课程需要前置技能c[i]至少达到lv1[i]等级,效果是技能d[i]达到lv2[i]等级,花费w[i]. 输出最小花费使得全技能满级(初始全技能0 ...
- hdu3072 强连通+最小树形图
题意:有一个人他要把一个消息通知到所有人,已知一些通知关系:A 能通知 B,需要花费 v,而又知道,如果某一个小团体,其中的成员相互都能直接或间接通知到,那么他们之间的消息传递是不需要花费的,现在问这 ...
- HDU4966 GGS-DDU(最小树形图)
之前几天想着补些算法的知识,学了一下最小树形图的朱刘算法,不是特别理解,备了份模板以备不时之需,想不到多校冷不丁的出了个最小树形图,没看出来只能表示对算法不太理解吧,用模板写了一下,然后就过了.- - ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
- HDU ACM 2121 Ice_cream’s world II (无根最小树形图)
[解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...
- Uva 11183 - Teen Girl Squad (最小树形图)
Problem ITeen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n ...
- POJ 3164 Command Network (最小树形图)
[题目链接]http://poj.org/problem?id=3164 [解题思路]百度百科:最小树形图 ]里面有详细的解释,而Notonlysucess有精简的模板,下文有对其模板的一点解释,前提 ...
随机推荐
- python安装模块的时候报错error: command 'gcc' failed with exit status 1
[情况] 在写Python代码的时候,需要用到psutil模块,需要安装. 但是在安装时,报错:error: command 'gcc' failed with exit status 1 [解决步骤 ...
- BeanUtils封装对象时一直提示ClassNotFoundException:org.apache.commons.beanutils.BeanUtils
导包明明正确了,依赖包也全都导对了,还是出错. 困扰了3天. 后来看到这篇博文,https://blog.csdn.net/yanshaoshuai/article/details/81624890 ...
- oracle 一个网站
http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html
- 集合遍历过程iterator, 添加删除元素报异常
list set 遍历过程中添加或者删除元素,报异常. 使用iterator 也会报异常 ConcurrentModificationException remove只能用迭代器的remove,而 ...
- POJ 1392 Ouroboros Snake(数位欧拉)
题目链接:http://poj.org/problem?id=1392 题目大意:题意看的我头痛,其实跟HDU2894差不多,但是这题要求输出这条路径上第k个数,而不是输出路径. 解题思路:也跟HDU ...
- Gitlab部署及汉化操作
一.简介 GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. GitLab拥有与Github类似的功能 ...
- web_reg_save_param_regexp函数的用法
关联从服务器返回的所有的内容: 本例通过一个使用HTTP/HTML协议发送.获取服务器数据的vuser脚本,分析LoadRunner如何进行HTTP关联. 下面这个例子包括两个事务:上传数据到服务器. ...
- Nodejs 接收RabbitMQ消息
参考官方地址:https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html 关于C#消息发送端,请参考<c# RabbitMQ ...
- thinkphp5.0目录结构
下载最新版框架后,解压缩到web目录下面,可以看到初始的目录结构如下: project 应用部署目录 ├─application 应用目录(可设置) │ ├─common 公共模块目录(可更改) │ ...
- 关于button标签会刷新页面的问题
当button标签在form表单里面时,这时点击button按钮会提交表单刷新页面. <form action=""> <button>点击</but ...