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/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");
}
}
上一篇:CAS 与.net 集成的 “循环重定向”问题分析


下一篇:JSR 整理、翻译