• POJ2735/Gym 100650E Reliable Nets dfs


    Problem E: Reliable Nets
    You’re in charge of designing a campus network between buildings and are very worried about its
    reliability and its cost. So, you’ve decided to build some redundancy into your network while keeping it
    as inexpensive as possible. Specifically, you want to build the cheapest network so that if any one line
    is broken, all buildings can still communicate. We’ll call this a minimal reliable net.

    Input
    There will be multiple test cases for this problem. Each test case will start with a pair of integers n
    (≤ 15) and m (≤ 20) on a line indicating the number of buildings (numbered 1 through n) and the
    number of potential inter-building connections, respectively. (Values of n = m = 0 indicate the end of
    the problem.) The following m lines are of the form b1 b2 c (all positive integers) indicating that it costs
    c to connect building b1 and b2. All connections are bidirectional.
    Output
    For each test case you should print one line giving the cost of a minimal reliable net. If there is a
    minimal reliable net, the output line should be of the form:
    The minimal cost for test case p is c.
    where p is the number of the test case (starting at 1) and c is the cost. If there is no reliable net possible,
    output a line of the form:
    There is no reliable net possible for test case p.
    Sample Input
    4 5
    1 2 1
    1 3 22015-08-19
    2 4 2
    3 4 1
    2 3 1
    2 1
    1 2 5
    0 0
    Sample Output
    The minimal cost for test case 1 is 6.
    There is no reliable net possible for test case 2.

    题意:

       给你一个图,找出一个最小权和的经过所有点的环;

    题解:

      数据小直接dfs找路,判断一下更新ans就好了

    ///by:1085422276
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <queue>
    #include <typeinfo>
    #include <map>
    #include <stack>
    typedef __int64 ll;
    #define inf 0x7fffffff
    using namespace std;
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //**************************************************************************************
    ll t,n,m,head[11111],vis[11111],vd[11111];
    ll ans,sum;
    struct ss
    {
        ll to,next;
        ll w;
    }e[300010];
    void init()
    {
        t=1;
        memset(head,0,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(vd,0,sizeof(vd));
    }
    void add(ll u,ll v,ll c)
    {
         e[t].to=v;
         e[t].w=c;
         e[t].next=head[u];
         head[u]=t++;
    }
    void boo()
    {
        for(ll i=1;i<=n;i++)if(!vis[i])return;
        ans=min(sum,ans);
    }
    void dfs(ll x)
    {
        if(x==1)
        {
            boo();
        }
        for(ll i=head[x];i;i=e[i].next)
        {
            if(!vd[i])
            {
                if(i%2)vd[i+1]=1;else vd[i-1]=1;
                int bb=vis[e[i].to];
                vis[e[i].to]=1;
                vd[i]=1;
                sum+=e[i].w;
                //printf("   %I64d---->%I64d
    ",x,e[i].to);
                dfs(e[i].to);
                sum-=e[i].w;
                 vis[e[i].to]=bb;
                 vd[i]=0;
                   if(i%2)vd[i+1]=0;else vd[i-1]=0;
            }
        }
    }
    int main()
    {
    ll oo=1;
        while(scanf("%I64d%I64d",&n,&m)!=EOF)
        {
            ll a,b,c;
            if(n==0&&m==0)break;
            init();
            for(ll i=1;i<=m;i++){
                scanf("%I64d%I64d%I64d",&a,&b,&c);
                //if(hash[a][b])continue;
                add(a,b,c);
                add(b,a,c);
            }
            ans=inf;
            sum=0;
            dfs(1ll);
            if(n==1||n==2)ans=inf;
            if(m==1)ans=inf;
            if(ans==inf){
                printf("There is no reliable net possible for test case %I64d.
    ",oo++);
            }
            else {
                printf("The minimal cost for test case %I64d is %I64d.
    ",oo++,ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    NGINX将PHP带参数的URL地址重定向二级或多级域名访问
    mysql优化 ON DUPLICATE KEY UPDATE
    修改Nginx配置文件来隐藏index.php
    Redis 如何保持和MySQL数据一致【二】
    Redis 如何保持和MySQL数据一致【一】
    Redis计算地理位置距离-GeoHash
    了解一下Redis队列【缓兵之计-延时队列】
    来了解一下Redis的分布式锁
    Kafka学习之(七)搭建kafka可视化服务Kafka Eagle
    include与jsp:include区别
  • 原文地址:https://www.cnblogs.com/zxhl/p/4741113.html
Copyright © 2020-2023  润新知