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

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

最新文章

  1. Ubuntu上Grafana 监控 Docker的技巧
  2. (原) 2.2 ZkClient使用
  3. android 的闪屏效果
  4. nginx fastcgi buffers影响页面输出数据大小记录
  5. centos 4.4配置使用 and Nutch搜索引擎(第1期)_ Nutch简介及安装
  6. hdu4639 hehe 递推
  7. 用php怎样将图片gif转化为jpg
  8. Java并发-线程安全性
  9. Java中数组的插入,删除,扩张
  10. nginx与fastdfs配置详解与坑
  11. java 日志脱敏框架 sensitive-新版本0.0.2-深度拷贝,属性为对象和集合的支持
  12. Fetch API &amp; Delete &amp; HTTP Methods
  13. 莫烦scikit-learn学习自修第三天【通用训练模型】
  14. CSS 中的 BFC,IFC,GFC和FFC
  15. Javascript 中ajax实现前台向后台交互
  16. BASIC-24_蓝桥杯_龟兔赛跑预测
  17. siebel切换数据源
  18. k8s1.8 ingress 配置
  19. Windows下Visual Studio2017之AI环境搭建
  20. 第一章Bootstrap简介

热门文章

  1. Inno Setup入门(八)&mdash;&mdash;有选择性的安装文件
  2. su -无反应
  3. MVC 和 MVVM
  4. UIActivityIndicatorView的详细使用(加载菊花)
  5. js入门实例
  6. 红帽linux忘记root密码的配置
  7. UVA - 1347 Tour(DP + 双调旅行商问题)
  8. HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)
  9. #ifndef 和 #endif
  10. Light OJ 1008