zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    @EnableTransactionManagement的使用
    Spring事务管理之几种方式实现事务
    instr和like的使用区别
    linux查看服务安装目录redis
    struts2的结果类型
    ajax 的 get 方式
    数据库隔离级别
    数据库隔离级别
    input from 表单提交 使用 属性 disabled=&quot;disabled&quot; 后台接收不到name=&quot;username&quot;的值
    Path Sum
  • 原文地址:https://www.cnblogs.com/zxhl/p/4741113.html
Copyright © 2011-2022 走看看