uva11183 Teen Girl Squad(最小树形图)

Problem I
Teen Girl Squad 

Input: Standard Input

Output: Standard Output

Time limit: 3.000 seconds

 

-- 3 spring rolls please.
-- MSG'D!!
-- Oh! My stomach lining!

Strong Bad

You are part of a group of n teenage girls armed with cellphones. You have some news you want to tell everyone in the group. The problem is that no two of you are in the same room, and you must communicate using only cellphones. What's worse is that due to excessive usage, your parents have refused to pay yourcellphone bills, so you must distribute the news by calling each other in the cheapest possible way. You will call several of your friends, they will call some of their friends, and so on until everyone in the group hears the news.

Each of you is using a different phone service provider, and you know the price of girl A calling girl B for all possible A and B. Not all of your friends like each other, and some of them will never call people they don't like. Your job is to find the cheapest possible sequence of calls so that the news spreads from you to all n-1 other members of the group.

Input

The first line of input gives the number of cases, (N<150). N test cases follow. Each one starts with two lines containing n  (0<= n<=1000)  and m  (0 <=  m <= 40,000) . Girls are numbered from 0 to  n-1 , and you are girl 0. The next m lines will each contain 3 integers, uv and w, meaning that a call from girl u to girl v costs w cents (0 <=  w <= 1000) . No other calls are possible because of grudges, rivalries and because they are, like, lame. The input file size is around 1200 KB.

 

Output

For each test case, output one line containing "Case #x:" followed by the cost of the cheapest method of distributing the news. If there is no solution, print "Possums!" instead.

 

Sample Input                                  Output for Sample Input

4
2
1
0 1 10
2
1
1 0 10
4
4
0 1 10
0 2 10
1 3 20
2 3 30
4
4
0 1 10
1 2 20
2 0 30
2 3 100
Case #1: 10
Case #2: Possums!
Case #3: 40
Case #4: 130
 

Problem setter: Igor Naverniouk

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=65&page=show_problem&problem=2124

分析:这题也是最小树形图,而且边的权值范围不大,继续用基数排序优化,这个复杂度,又刷到Rank1.。。。

留念~~~

RankingSubmissionUserRun TimeLanguageSubmission Date
1 9224799 Pira 0.028 C++ 2011-09-05 06:05:29
由此可见只要边权值范围小的,就用这个模板了。。。。

代码:

#include<cstdio>
using namespace std;
const int mm=44444;
const int mn=1111;
struct edge
{
    int s,t,w;
}g[mm],h[mm];
int head[mm],next[mm];
int p[mn],q[mn],mark[mn],fp[mn],from[mn],vis[mn],in[mn],w[mn],ans,sum;
int i,j,k,n,m,e,r,mw,t,cas=0;
bool huan;
inline void addedge(int u,int v,double c)
{
    g[e].s=u,g[e].t=v,g[e].w=c,next[e]=head[u],head[u]=e++;
    if(c>mw)mw=c;
}
void dfs(int u)
{
    ++sum,vis[u]=1;
    for(int i=head[u];i>=0;i=next[i])
        if(!vis[g[i].t])dfs(g[i].t);
}
inline void init(int& a)
{
    char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    for (a=0; ch>='0'&&ch<='9'; ch=getchar()) a=a*10+ch-48;
}
void mysort()
{
    int i;
    for(i=0;i<=n;++i)q[i]=0;
    for(i=0;i<=mw;++i)p[i]=0;
    for(i=0;i<e;++i)++q[g[i].t],++p[g[i].w];
    for(i=1;i<=n;++i)q[i]+=q[i-1];
    for(i=n;i>0;--i)q[i]=q[i-1];
    q[0]=0;
    for(i=1;i<=mw;++i)p[i]+=p[i-1];
    for(i=mw;i>0;--i)p[i]=p[i-1];
    p[0]=0;
    for(i=0;i<e;++i)h[p[g[i].w]++]=g[i];
    for(i=0;i<e;++i)g[q[h[i].t]++]=h[i];
}
int main()
{
    init(t);
    while(t--)
    {
        init(n),init(m),--n;
        for(i=e=mw=0;i<=n;++i)head[i]=-1;
        while(m--)
        {
            init(i),init(j),init(k);
            if(i!=j)addedge(i,j,k);
        }
        for(sum=i=0;i<=n;++i)vis[i]=0;
        dfs(0);
        if(sum<n+1)
        {
            printf("Case #%d: Possums!\n",++cas);
            continue;
        }
        mysort();
        for(i=0;i<=n;++i)fp[i]=p[i]=-1,in[i]=vis[i]=0,mark[i]=i;
        for(i=0;i<e;++i)
            if(p[g[i].t]<0)p[g[i].t]=i;
        huan=1,ans=sum=0;
        while(huan)
        {
            huan=0;
            for(i=1;i<=n;++i)
                if(fp[j=mark[i]]>=0)
                {
                    if(fp[i]<0)in[i]+=w[j],mark[i]=mark[mark[i]];
                    else
                    {
                        in[i]+=w[i],ans+=w[i];
                        if(g[++p[fp[i]]].t!=fp[i])p[fp[i]]=-1;
                    }
                }
            for(i=0;i<=n;++i)fp[i]=-1,vis[i]=0;
            for(i=1;i<=n;++i)
                if(p[i]>=0)
                {
                    if(fp[j=mark[i]]<0||(fp[j]>=0&&w[j]>g[p[i]].w-in[i]))
                       w[j]=g[p[i]].w-in[i],fp[j]=i,from[j]=mark[g[p[i]].s];
                }
            for(sum=0,i=1;i<=n;++i)
                if(fp[i]>=0)sum+=w[i];
            for(i=1;i<=n;++i)
                if(!vis[i])
                {
                    r=0,j=i;
                    while(j>0&&vis[j]>=0)
                    {
                        if(vis[j]>0)
                        {
                            huan=1;
                            while(q[--r]!=j)mark[q[r]]=j,vis[q[r]]=-1;
                            vis[j]=-1;
                        }
                        else if(!vis[j])vis[q[r++]=j]=1;
                        if(fp[j]>=0)j=from[j];
                        else j=-1;
                    }
                    while(r--)vis[q[r]]=fp[q[r]]=-1;
                }
        }
        printf("Case #%d: %d\n",++cas,ans+sum);
    }
    return 0;
}

普通版本1

#include<cstdio>
#include<cmath>
#define type int
using namespace std;
const int mm=44444;
const int mn=1111;
const int oo=1000000000;
type c[mm],in[mn],ans;
int s[mm],t[mm],id[mn],pre[mn],q[mn];
type Directed_MST(int root,int NV,int NE)
{
    type ret=0;
    int i,j,u,v,cnt,r;
    while(1)
    {
        for(i=0;i<=NV;++i)in[i]=oo;
        for(i=0;i<NE;++i)
            if((u=s[i])!=(v=t[i])&&in[v]>c[i])
                pre[v]=u,in[v]=c[i];
        pre[root]=-1,in[root]=0;
        for(i=1;i<=NV;++i)
            if(in[i]==oo)return -1;
        for(i=1;i<=NV;++i)id[i]=-1,ret+=in[i];
        for(cnt=0,i=1;i<=NV;++i)
            if(id[i]<0)
            {
                r=0,j=i;
                while(j>=0&&id[j]<0)
                {
                    if(id[j]==-2)
                    {
                        id[j]=++cnt;
                        while(q[--r]!=j)id[q[r]]=cnt;
                    }
                    else id[q[r++]=j]=-2,j=pre[j];
                }
                while(r--)id[q[r]]=++cnt;
            }
        if(cnt==NV)break;
        for(i=0;i<NE;++i)
        {
            j=t[i],s[i]=id[s[i]],t[i]=id[t[i]];
            if(s[i]!=t[i])c[i]-=in[j];
        }
        NV=cnt;
        root=id[root];
    }
    return ret;
}
int main()
{
    int n,m,e,T,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m),--n;
        e=0;
        while(m--)scanf("%d%d%d",&s[e],&t[e],&c[e]),e+=(s[e]!=t[e]);
        ans=Directed_MST(0,n,e);
        if(ans<0)printf("Case #%d: Possums!\n",++cas);
        else printf("Case #%d: %d\n",++cas,ans);
    }
    return 0;
}


普通版本2

#include<cstdio>
#include<cmath>
#define type int
using namespace std;
const int mm=44444;
const int mn=1111;
type c[mm],in[mn],w[mn],ans;
int s[mm],t[mm],id[mn],pre[mn],q[mn],vis[mn];
type Directed_MST(int root,int NV,int NE)
{
    type ret=0,sum=0,tmp;
    int i,j,u,v,r;
    bool huan=1;
    for(i=0;i<=NV;++i)in[i]=0,id[i]=i,pre[i]=-1;
    while(huan)
    {
        for(i=0;i<=NV;++i)
            if(pre[j=id[i]]>=0)
            {
                if(pre[i]<0)in[i]+=w[j],id[i]=id[j];
                else in[i]+=w[i],ret+=w[i];
            }
        for(i=0;i<=NV;++i)pre[i]=-1,vis[i]=0;
        for(i=0;i<NE;++i)
            if((u=id[s[i]])!=(v=id[t[i]])&&(w[v]>(tmp=c[i]-in[t[i]])||pre[v]<0))
                pre[v]=u,w[v]=tmp;
        for(i=1;i<=NV;++i)
            if(i!=root&&id[i]==i&&pre[i]<0)return -1;
        for(pre[root]=-1,sum=i=0;i<=NV;++i)
            if(pre[i]>=0)sum+=w[i];
        for(i=huan=0;i<=NV;++i)
            if(!vis[i])
            {
                r=0,j=i;
                while(j>=0&&vis[j]>=0)
                {
                    if(vis[j]>0)
                    {
                        while(q[--r]!=j)id[q[r]]=j,vis[q[r]]=-1;
                        huan=1,vis[j]=-1;
                    }
                    else vis[q[r++]=j]=1,j=pre[j];
                }
                while(r--)vis[q[r]]=pre[q[r]]=-1;
            }
    }
    return ret+sum;
}
int main()
{
    int n,m,e,T,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m),--n;
        e=0;
        while(m--)scanf("%d%d%d",&s[e],&t[e],&c[e]),e+=(s[e]!=t[e]);
        ans=Directed_MST(0,n,e);
        if(ans<0)printf("Case #%d: Possums!\n",++cas);
        else printf("Case #%d: %d\n",++cas,ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值