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. 《DSP using MATLAB》示例 Example 9.14

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  2. Ext.js基础

    第一章:Ext.js基础 好书推荐 Javascript设计模式 征服ajax web 2.0开发技术详解 简介 基础要求 了解HTML.CSS.熟练JS.JS的OOP.AJAX JSP/PHP/AS ...

  3. 【LGR-051】洛谷9月月赛

    [LGR-051]洛谷9月月赛 luogu 签到题 description 给出\(K\)和质数\(m\),求最小的\(N\)使得\(111....1\)(\(N\)个\(1\))\(\equiv k ...

  4. oracle行跟踪(基于行跟踪的ROWDEPENDENCIES ORA_ROWSCN信息)

    在Oracle 10g中的引入了ORA_ROWSCN伪列新特性.基于此种伪列所提供的信息,我们可以方便地找出某个数据块或某一个行最近被修改的时间戳.在默认情况下,10g下表会以非行依赖性(NOROWD ...

  5. 网站配色、网站模板网址 UE样式 metro报表

    http://www.colourlovers.com http://unmatchedstyle.com网站模板目录    花瓣 http://huaban.com/    油表 http://fl ...

  6. .net下所有DLL(API)查询,转换C#代码

    地址: http://www.pinvoke.net/default.aspx/coredll.SetDevicePower 实例: SetDevicePower (coredll)   coredl ...

  7. 【ZZ】MySQL 索引优化全攻略 | 菜鸟教程

    MySQL 索引优化全攻略 http://www.runoob.com/w3cnote/mysql-index.html

  8. PHP实现连接设备、通讯和发送命令的方法

    这篇文章主要介绍了PHP实现连接设备.通讯和发送命令的方法,涉及php基于socket实现设备连接及数据通信的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下   本文实例讲述了PHP实现连接设备 ...

  9. CFGym 101490E 题解

    一.题目链接 http://codeforces.com/gym/101490 二.题面 三.题意 给你一个图,n个点,m条边,一个x,从顶点1走到顶点n.假设从顶点1走到顶点n的最短路为d,x代表你 ...

  10. Check failed: mdb_status == 0 (2 vs. 0) No such file or directory

    运行  ./examples/mnist/train_lenet.sh  时,碰到了这个问题. 一定是路径问题!!!仔细查看prototxt文件里面的各种路径!!! 解决方案: 把.prototxt里 ...