Ice_cream’s world II

http://acm.hdu.edu.cn/showproblem.php?pid=2121

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6736    Accepted Submission(s): 1779

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

3 1
0 1 1

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

 
Sample Output
impossible
 
40 0
 
Author
Wiskey
 
Source
 
不懂为什么用了注释中的代码会WA....
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
const int INF=0x3f3f3f3f;
#define MAXN 1002
#define MAXM 10005
using namespace std;
struct Edge{
int u,v;
int cost;
}edge[MAXM]; int pre[MAXN],id[MAXN],visit[MAXN];
int in[MAXN];
int minroot; int zhuliu(int root,int n,int m){
int res=;
while(){
for(int i=;i<n;i++){
in[i]=INF;
}
for(int i=;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;
if(edge[i].u==root){
minroot=i;
}
}
}
for(int i=;i<n;i++){
if(i!=root && in[i]==INF){
return -;
}
}
int tn=;
memset(id,-,sizeof(id));
memset(visit,-,sizeof(visit));
in[root]=;
for(int i=;i<n;i++){
res+=in[i];
int v=i;
while(visit[v]!=i&&id[v]==-&&v!=root){
visit[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-){
for(int u=pre[v];u!=v;u=pre[u]){
id[u]=tn;
}
id[v]=tn++;
}
}
if(tn==) break;
for(int i=;i<n;i++){
if(id[i]==-){
id[i]=tn++;
}
}
/* for(int i=0;i<m;){
int 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];
}
else{
swap(edge[i],edge[--m]);
}
}*/
for(int i=;i<m;i++){
int 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];
}
}
n=tn;
root=id[root];
}
return res;
} int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
int sum=;
for(int i=;i<m;i++){
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].cost);
edge[i].u++;
edge[i].v++;
sum+=edge[i].cost;
}
sum++;
for(int i=m;i<n+m;i++){
edge[i].u=,edge[i].v=i-m+,edge[i].cost=sum;
}
int ans=zhuliu(,n+,m+n);
if(ans==-||ans>=*sum){
puts("impossible");
}
else{
printf("%d %d\n",ans-sum,minroot-m);
}
printf("\n");
}
}

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. hdu2121 Ice_cream’s world II 最小树形图(难)

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

  5. hdu2121 最小树形图的虚根

    /* 最小树形图的第二题,终于有了一些理解 具体看注释 */ /* 无定根的最小树形图 建立虚root 每次只找最短的那条入边 最小树形图理解: 第一步:寻找最短弧集E:扫一遍所有的边,找到每个点权值 ...

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

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

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

  8. HDU ACM 2121 Ice_cream’s world II (无根最小树形图)

    [解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...

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

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

随机推荐

  1. 折叠纸片PFold.js

    PFold.js是一款折叠纸片插件,支持定义折叠纸牌数量.折叠动画效果.折叠方向,而且还支持折叠结束后回调方法. 在线实例 效果一 效果二 效果三 使用方法 <div id="uc-c ...

  2. python实践报错:SyntaxError: Non-ASCII character

    报错: File "C:\001myWorkspace\001myWork\workspace2\MyFirstPython\src\demo4\demo4-2.py", line ...

  3. CH3401 石头游戏

    题意 3401 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (1≤n,m≤8) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数字指明 ...

  4. linux 下mongodb 3.2.5单机版安装

    mongodb3.0.x的安装教程网上很多,这里主要介绍3.2.5的安装 linux iso 在\\10.10.10.1\ShareDoc\User\yipengzhi\ISO\Centos7.0   ...

  5. Dictionary字典类介绍

    说明     必须包含名空间System.Collection.Generic      Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)      键必须是唯一的,而值不 ...

  6. /etc/sysctl.conf参数解释(转)

    来自<深入理解Nginx模块开发与架构解析> P9 #表示进程(例如一个worker进程)可能同时打开的最大句柄数,直接限制最大并发连接数fs.file max = 999999 #1代表 ...

  7. 使用JS 加入收藏,设为首页.

    <script type="text/javascript" language="javascript"> function AddFavorite ...

  8. 01:初识Redis

    付磊和张益军两位大咖写的葵花宝典(Redis开发和运维)学习笔记. 一.初识Redis 1.redis简介 Redis是一种基于键值对(key-value)的NoSQL数据库,与很多键值对数据库不同的 ...

  9. 使用GridFsTemplate在Mongo中存取文件

      Maven依赖(还有一些springboot需要的) <parent> <groupId>org.springframework.boot</groupId> ...

  10. Python2.X如何将Unicode中文字符串转换成 string字符串

    Python2.X如何将Unicode中文字符串转换成 string字符串   普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:unicodestring = u&q ...