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

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;
}

转载于:https://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. STM32串行通信USART解说笔记
  2. ASP.NET中连接Sqlserver数据库时提示:未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc
  3. CodeForces 1191A---Tokitsukaze and Enhancement
  4. 操作系统复习笔记 06 CPU Scheduling CPU调度
  5. Spring Boot 单元测试详解+实战教程
  6. Qt工作笔记-ListWidget拖动(拖拽)到QGraphicsScene【补坑】【Qt视图框架补坑】
  7. C#LeetCode刷题之#551-学生出勤纪录 I​​​​​​​(Student Attendance Record I)
  8. jenkins中使用rsync, scp命令
  9. 【编辑器】Vim学习笔记
  10. a标签中执行js函数
  11. redhat linux 设置ip,REDHAT LINUX企业版更改IP地址,网关,DNS和MAC地址----字符界面
  12. 【pyqt5学习】——日历控件calendarWidget设置单元格格式(前景、背景)、日历控件属性编辑
  13. Redis数据结构-sds
  14. 鸿蒙方舟UI开发框架-eTS状态管理
  15. oswatch的安装和使用(转)
  16. 给定两个数组arrx和arry,长度都为N。代表二维平面上有N个点,第i个点的x 坐标和y坐标分别为arrx[i]和arry[i],返回求一条直线最多能穿过多少个点?
  17. UE4 蓝图学习 FlipFlop
  18. 分享一个SlideShare:《做卓有成效的程序员》
  19. 基于webmagic的种子网站爬取
  20. 什么是循环依赖以及解决方式

热门文章

  1. jqurey ajax 的动态添加二级联动下拉菜单
  2. Samba 3.0.25 颁布发表
  3. Nginx配置wss访问实现微信小程序的websocket通信
  4. LeetCode OJ - Best Time to Buy and Sell Stock II
  5. 01c-1: 主流长远
  6. ZCTF2015 pwn试题分析
  7. Javascript预解析、作用域、作用域链
  8. C++ Primer 第4章数组和指针
  9. Windows Phone 7 使用Canvas Grid StackPanel进行布局管理
  10. Winform GDI+ 绘图