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. c# sql省市联动

    C# sql省市联动 USE [Book] GO ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ...

  2. mysql5.7 Your password does not satisfy the current policy requirements问题解决

    安装mysql5.7rpm包,在更改密码的时候,提示错误 这是由于Mysql5.7默认对于密码的要求强度较高,设置的密码过于简单不予通过.要解决这个问题,涉及到的参数有validate_passwor ...

  3. Java框架之spring 项目 附加之noteresult(status msg data uuid MD5)

    1.1创建类    实体类 NoteResult<T>  注意<T> private int status; private String msg; private T dat ...

  4. server12装.NET 3.5

    参考:https://support.microsoft.com/en-us/help/2734782/net-framework-3-5-installation-error-0x800f0906- ...

  5. LB 负载均衡的层次结构(转)

    http://blog.csdn.net/mindfloating/article/details/51020767 作为后端应用的开发者,我们经常开发.调试.测试完我们的应用并发布到生产环境,用户就 ...

  6. jmeter-Http信息头管理器

    今天遇到的问题是:一个报名接口,用户先要登录后,再去报名.而登录页面:用户名.密码.动态图形验证码,所以不能直接使用jmeter参数传值方法. 测试的时候,需要先登录,获取ticket后,才能去请求下 ...

  7. 最短路【洛谷P3393】 逃离僵尸岛

    P3393 逃离僵尸岛 题目描述 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被 ...

  8. react常用开发工具

    https://www.cnblogs.com/vipstone/p/7125338.html

  9. maven 中 指定jdk 和 编译编码,仓库位置

    <!-- 配置编译选项 --> <profile> <id>jdk1.8</id> <activation> <activeByDef ...

  10. day_02 循环格式化输出编码运算符

    1.while循环 语法 while 条件: 循环体 else: 当条件不成立的时候执行这里,和break没关系 如果循环是通过break退出的. 那么while后面的else将不会被执行, 只有在w ...