Ice_cream’s world II

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

3 1
0 1 1
4 4
0 1 10
0 2 10
1 3 20
2 3 30
 

Sample Output

impossible
40 0
 
 
题目大意:n个城市,m条有向边。问你是否存在一个城市,能通向其他所有城市且权值和最大。如果存在多个这样的城市,那么输出城市编号最小的那个。如果不存在,输出impossible。
 
解题思路:不定根的最小树形图。我们考虑建立一个起点,这个起点跟所有其他的顶点连边,权值为城市中的所有边的权值和+1。然后就是用朱刘算法求解了。
 
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long INT;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Edge{
int from,to;
int dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
int In[maxn];
int ansidx ;
INT Zhuliu(int root,int n,int m){
INT ret = 0;
int u,v;
while(true){
for(int i = 0; i < n; i++){
In[i] = INF;
}
for(int i = 0; i < m; i++){
Edge &e = edges[i];
u = e.from; v = e.to;
if(In[v] > e.dist && u != v){
pre[v] = u;
if(u == root){ //记录边的编号,这个编号-m就是点编号,因为我们加边是从顶点从小到大
ansidx = i;
}
In[v] = e.dist;
}
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(In[i] == INF)
return -1;
}
In[root] = 0;
int cntcir = 0;
memset(vis,-1,sizeof(vis));
memset(ID,-1,sizeof(ID));
for(int i = 0; i < n; i++){
ret += 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(u = pre[v]; u != v; u = pre[u]){
ID[u] = cntcir;
}
ID[v] = cntcir++;
}
}
if(cntcir == 0){
break;
}
for(int i = 0; i < n; i++){
if(ID[i]==-1){
ID[i] = cntcir++;
}
}
for(int i = 0; i < m; i++){
v = edges[i].to;
Edge & e = edges[i];
e.from = ID[e.from];
e.to = ID[e.to];
if(e.from != e.to){
e.dist -= In[v];
}
}
n = cntcir;
root = ID[root];
}
return ret;
}
int main(){
int n,m, T, cas = 0;
while(scanf("%d%d",&n,&m)!=EOF){
int a,b,c;
INT sumd = 0;
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
a++,b++;
edges[i].from = a;
edges[i].to = b;
if(a == b){
edges[i].dist = INF;
continue;
}
edges[i].dist = c;
sumd += c;
}
for(int i = 0; i < n;i++){
edges[m+i].from = 0;
edges[m+i].to = i + 1;
edges[m+i].dist = sumd + 1;
}
INT res = Zhuliu(0,n+1,m+n);
if(res == -1||res > 2*sumd+1){
puts("impossible");
}else{
printf("%lld %d\n",res - sumd -1,ansidx - m);
}puts("");
}
return 0;
} /*
3 2
0 1 2
1 2 3 3 0 */

  

 

HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】的更多相关文章

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

    开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

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

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

  3. 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 ...

  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. Django会话,用户和注册之session

    鉴于cookie的不安全,django自带的session框架会帮我们搞定这些问题 你可以用session 框架来存取每个访问者任意数据, 这些数据在服务器端存储,并对cookie的收发进行了抽象. ...

  2. [内容分享]粗略判断Shader每条代码的成本

    https://mp.weixin.qq.com/s/Vyn1bKaBMHommxbnFPPQeg Unity对Shader文件进行编译的时候,DX9和DX11的版本会直接生成汇编码. ?   len ...

  3. asp如何让panel居中

    把panel放在一个table的td中,并把position设为relative,就可以在设计中直接调整它的位置了

  4. 【转】windows server 2012 R2搭建IIS服务器

    源地址:http://blog.csdn.net/microsoft_wu/article/details/46521017

  5. NOIP2018初赛总结(提高组)(试题+答案+简要解析)

    NOIP2018初赛总结(提高组) 更新完毕(纯手敲),如果有错误请在下面留言 单选题 T1.下列四个不同进制的数中,与其它三项数值上不相等的是 A.\((269)_{16}\) B.\((617)_ ...

  6. PHP删除目录下的空目录

    function rm_empty_dir($path){       if(is_dir($path) && ($handle = opendir($path))!==false){ ...

  7. java.sql.Date/ java.util.Date/ java.util.Calendar 用法与区别

    在 JDK API  这样解释," 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串.不过,这些函数的 ...

  8. Android模拟器手动设置经纬度坐标

    第一种方式可以在eclipse的DDMS中的Emulator control中设置,如下图 另一种是在cmd中输入telnet localhost 5554(注:5554是模拟器在本机的端口,有可能不 ...

  9. Mybatis学习笔记(五) —— Mapper.xml(输入映射和输出映射)

    一.parameterType(输入类型) 1.1 传递简单类型 <!-- 根据用户id查询用户 --> <select id="queryUserById" p ...

  10. eclipse gradle 找不到依赖解决办法

    右击工程,选择gradle  在点击Refresh Gradle Project 即可,..不得不说,gradle 在eclipse 下真没maven 好用.....