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/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 
 
威士忌   |   We have carefully selected several similar
problems for you:  2120 2122 1222 4009 2064 
 
 
没有根的最小树形图
新建虚拟节点,向所有点连权值为 边权和+1的边
最后再减去这条边
 
记录根节点时,记录虚拟边的编号,不要记录虚拟边指向的点,因为缩环之后点的编号有所改变
 
#include<cstdio>
#include<cstring>
#define N 1005
#define M 10005
#define inf 2e9
using namespace std;
struct node
{
int u,v,w;
}e[M+N];
int in[N],pre[N],vis[N],col[N],id[N];
int ROOT;
int n,m;
int directed_MST()
{
int tot=n+,root=,ans=,cirnum=,to;
while()
{
for(int i=;i<tot;i++) in[i]=inf;
for(int i=;i<=m;i++)
if(e[i].u!=e[i].v && in[e[i].v]>e[i].w)
{
in[e[i].v]=e[i].w;
pre[e[i].v]=e[i].u;
if(e[i].u==root) ROOT=i;
}
cirnum=;
memset(vis,-,sizeof(vis));
memset(col,-,sizeof(col));
in[root]=;
for(int i=;i<tot;i++)
{
ans+=in[i];
to=i;
while(vis[to]!=i && col[to]==- && to!=root)
{
vis[to]=i;
to=pre[to];
}
if(to!=root && col[to]==-)
{
for(int nt=pre[to];nt!=to;nt=pre[nt])
col[nt]=cirnum;
col[to]=cirnum++;
}
}
if(!cirnum) return ans;
for(int i=;i<tot;i++)
if(col[i]==-) col[i]=cirnum++;
for(int i=;i<=m;i++)
{
to=e[i].v;
e[i].u=col[e[i].u];
e[i].v=col[e[i].v];
if(e[i].u!=e[i].v) e[i].w-=in[to];
}
tot=cirnum;
root=col[root];
}
return ans;
}
int main()
{
int tot,sum;
int u,v,w,ans,tmp;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot=sum=;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(u!=v)
{
u++; v++;
e[++tot].u=u; e[tot].v=v; e[tot].w=w;
sum+=w;
}
}
tmp=tot;
for(int i=;i<=n;i++)
{
e[++tot].u=; e[tot].v=i; e[tot].w=sum+;
}
m=tot;
ans=directed_MST();
if(ans>=*(sum+)) printf("impossible\n\n");
else printf("%d %d\n\n",ans-(sum+),ROOT-tmp-);
}
}
上一篇:【DB笔试面试619】在Oracle中,举例说明“连接因式分解(Join factorization,JF)”查询转换。...


下一篇:11gr2全外连接优化执行计划(二)