題目:
Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1610 Accepted Submission(s): 354
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 www.2cto.com
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
HDU 2007-10 Programming Contest_WarmUp
Recommend
威士忌
分析與總結:
不定根最小樹形圖問題,解決方法是設計一個“虛擬新根點”,這個從這個結點出發可以到達所有點,並且權值是一個很大的值(比所有權值之和大),然後找這個跟結點是否有最小樹形圖。
而求出真正的跟結點的方法很巧妙, 利用邊的位置與虛擬結點的關系。
代碼:
[cpp]
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int VN = 1005;
const int INF = 0x7fffffff;
int ans_root;
template<typename Type>
class Directed_MST{
public:
void init(int _n){
n=_n+1; size=0; ans=0;
}
void insert(int u, int v, Type _w){
E[size++].set(u,v,_w);
}
Type directed_mst(int root){
while(true){
// 1.找最小的前驅邊
for(int i=1; i<n; ++i)
in[i]=INF;
for(int i=0; i<size; ++i){
int u=E[i].u, v=E[i].v;
if(E[i].w < in[v] && u!=v){
pre[v] = u;
in[v] = E[i].w;
if(u==root){
ans_root = i; // 保存的是邊的位置i,而不是v
}
}
}
for(int i=1; i<n; ++i)if(i!=root){
if(in[i]==INF) return -1;
}
// 2.找環
int MXid = 1;
in[root] = 0;
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(id));
for(int i=1; i<n; ++i){
ans += in[i];
int v = i;
while(vis[v]!=i && id[v]==-1 && v!=root){
vis[v] = i;
v = pre[v];
}
if(v!=root && id[v]==-1){
for(int u=pre[v]; u!=v; u=pre[u]){
id[u] = MXid;
}
id[v] = MXid++;
}
}
if(MXid==1) break; //無環
for(int i=1; i<n; ++i)
if(id[i]==-1) id[i] = MXid++;
// 3.縮點,重新標記
for(int i=0; i<size; ++i){
int u=E[i].u, v=E[i].v;
E[i].u = id[u];
E[i].v = id[v];
if(E[i].u!=E[i].v) E[i].w -= in[v];
}
n = MXid;
root = id[root];
}
return ans;
}
private:
struct Edge{
int u,v;
Type w;
void set(int _u,int _v,Type _w){
u=_u,v=_v,w=_w;
}
}E[VN*VN/2];
Type ans; // 所求答案
Type in[VN];
int n; // 結點個數
int size; // 邊的數量
int pre[VN]; // 權值最小的前驅邊
int id[VN];
int vis[VN]; // 是在環中還是在環外
};
Directed_MST<int>G;
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
G.init(n+1);
int Max = 0;
for(int i=0; i<m; ++i){
int a,b;
int c;
scanf("%d%d%d",&a,&b,&c);
if(a==b)continue;
Max += c;
G.insert(a+1,b+1,c);
}
++Max;
// 把n+1設置為虛擬根結點
for(int i=m; i<m+n; ++i){
G.insert(n+1, i-m+1, Max); // 注意虛擬結點n+1與i的關系,當i>m之後,依次從虛擬根結點n+1出發
// 到達1,2,...,n.利用這個關系,當i>m時利用位置就可以確定m+1與其它結點的關系
// 後面要用這個關系輸出根結點
}
int ans = G.directed_mst(n+1);
if(ans==-1 || ans-Max>=Max)puts("impossible");
else printf("%d %d\n", ans-Max, ans_root-m);
puts("");
}
return 0;
}