开始学习最小树形图,模板题。

Ice_cream’s world II

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

【Problem Description】
After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing.
【Input】
Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.
【Output】
If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank.
【Sample Input】

【Sample Output】

impossible

【分析】

一道裸的最小树形图模板题,算法思路比较简单,但是写起来感觉很不优美...仅作为模板记录吧。

本题不确定起点,故在读入完数据之后建立一个虚根,把所有的点都与这个虚根相连,边权为所有边边权和+1(保证虚边足够长),最后减掉即可。

确定起点的题可以从模板中删掉这部分。

然后就是这个模板是从0开始存储...强迫症好不爽啊...

详细参见:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html

 /* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU 2121
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = ;
const int M = N*N; typedef struct nod
{
int a,b,c;
} node; node edge[M]; int predge[N],id[N],visit[N];//id用来标记圈的
int in[N];//入弧最小的 int ansroot; int dmst(int root,int n,int m)//n表示点数,m表示边数,root表示根
{
int u,v;
int ret=;
while(true)
{
for(int i=;i<n;i++) in[i]=INT_MAX;
for(int i=;i<m;i++)
{
if(edge[i].c<in[edge[i].b]&&edge[i].a!=edge[i].b)
{
predge[edge[i].b]=edge[i].a;//找出每个点的最小入弧
if(edge[i].a==root) ansroot=i;
in[edge[i].b]=edge[i].c;
}
}
for(int i=;i<n;i++)
{
if(i==root) continue;
if(in[i]==INT_MAX) return -;
}
in[root]=;
int cnt=;
memset(id,-,sizeof(id));
memset(visit,-,sizeof(visit));
for(int i=;i<n;i++)
{
ret+=in[i];//进行缩圈
v=i;
while(visit[v]!=i&&id[v]==-&&v!=root)
{
visit[v]=i;
v=predge[v];
}
if(v!=root&&id[v]==-)
{
for(u=predge[v];u!=v;u=predge[u])
id[u]=cnt;
id[v]=cnt++;
}
}
if (cnt==) break;
for (int i=;i<n;i++)
if (id[i]==-) id[i]=cnt++;
for (int i=;i<m;i++)
{
v=edge[i].b;//进行缩点,重新标记。
edge[i].a=id[edge[i].a];
edge[i].b=id[edge[i].b];
if (edge[i].a!=edge[i].b) edge[i].c-=in[v];
}
n=cnt;
root=id[root];
}
return ret;
}
int main()
{
freopen("2121.txt","r",stdin);
int n,m,m1;
while(scanf("%d%d",&n,&m)!=EOF)
{
int a,b;
int r=;
m1=m;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
r+=edge[i].c;
}
r++;
for(int i=;i<n;i++)
{
edge[m].a=n;
edge[m].b=i;
edge[m].c=r;
m++;
}
int ans=dmst(n,n+,m);
ansroot-=m1;//最小根对应的标号为i-m1
if(ans==-||ans>=*r) printf("impossible\n");
else printf("%d %d\n",ans-r,ansroot);
printf("\n");
}
return ;
}

HDU 2121 Ice_cream’s world II 最小树形图 模板的更多相关文章

  1. HDU 2121 Ice_cream’s world II 最小树形图

    这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...

  2. HDU2121 Ice_cream’s world II —— 最小树形图 + 不定根 + 超级点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 Ice_cream’s world II Time Limit: 3000/1000 MS (J ...

  3. HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

    Ice_cream’s world II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  4. HDU 2121 Ice_cream’s world II 不定根最小树形图

    题目链接: 题目 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  5. hdu 2121 Ice_cream’s world II (无定根最小树形图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...

  6. HDU - 2121 Ice_cream’s world II 无根最小树形图

    HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121 比较好的朱刘算法blog:https://blog.csdn.net/txl199 ...

  7. hdu 2121 Ice_cream’s world II

    Ice_cream’s world II http://acm.hdu.edu.cn/showproblem.php?pid=2121 Time Limit: 3000/1000 MS (Java/O ...

  8. hdu2121 Ice_cream’s world II 最小树形图(难)

    这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...

  9. hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】

    称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...

随机推荐

  1. hh monitor

    http://theholyjava.wordpress.com/2012/09/21/enabling-jmx-monitoring-for-hadoop-and-hive/ http://blog ...

  2. Java中获取路径的方法_自我分析

    就目前的我来说最常用的两种获取路径的方法是  class.getRecource(filename) 和 class.getclassloader.getRecource(filename) 这两者的 ...

  3. JSTL判断list的size()大小

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ tag ...

  4. 80-th Level Archeology

    80-th Level Archeology time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Spring Boot 系列教程8-EasyUI-edatagrid扩展

    edatagrid扩展组件 edatagrid组件是datagrid的扩展组件,增加了统一处理CRUD的功能,可以用在数据比较简单的页面. 使用的时候需要额外引入jquery.edatagrid.js ...

  6. UIActivityIndicatorView的详细使用(加载菊花)

    UIActivityIndicatorView实例提供轻型视图,这些视图显示一个标准的旋转进度轮.当使用这些视图时,最重要的一个关键词是小.20×20像素是大多数指示器样式获得最清楚显示效果的大小.只 ...

  7. jquery 仿购物车的加减数量

    <p>单价:3.95</p> <input id="min" name="" type="button" va ...

  8. POJ1734/Floyd求最小环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6647   Accepted: 2538 ...

  9. iOS拨打电话(三种方法)

    iOS拨打电话(三种方法)  查了很多地方的关于iOS程序拨打电话,大都不全,今天我总结了三种方法,各有不同,拿来给大家分享,希望给大家有所帮助 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯 ...

  10. UVALive 2053 Puzzlestan(深搜+技巧)

    这个题目的深搜形式,我也找出来了,dfs(i,j)表示第i个人选到了第j个物品,但是我却无限RE了,原因是我的viod型深搜太过暴力,我当时定义了一个计数器,来记录并限制递归的层数,发现它已经递归到了 ...