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. Speci?cally, 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\n",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.\n",oo++);
        }
        else {
            printf("The minimal cost for test case %I64d is %I64d.\n",oo++,ans);
        }
    }
    return 0;
}
时间: 2024-10-14 10:55:54

POJ2735/Gym 100650E Reliable Nets dfs的相关文章

ACM: Gym 100935G Board Game - DFS暴力搜索

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar

CodeForces Gym 100935G Board Game DFS

 Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boa

K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子树上的所有节点的权值的乘积x,及x的因数个数. 2.SEED:将节点u的权值乘以x. 思路: 比赛时少看了因数不大于13这句话,然后本题难度增加数倍,肝了两个小时都没肝出来,对不起队友啊,今天的组队训练赛实力背锅…… 这题一眼线段树,由于是对一棵子树进行处理,因此我们采用常规套路,借助dfs序将子树

Gym 100650H Two Ends DFS+记忆化搜索

Problem H: Two EndsIn the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, faceup, is written a positive integer. Players take turns removing a card from either end of the row andplacing the card in their pile.

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

很好的脑洞题:dfs+暴力 Gym - 101128A Promotions

http://codeforces.com/gym/101128 题目大意:给你一个a,b,e,p.有e个点,p条有向边,每条边为(x,y),表示x->y,每次我们都取出一个入度为0的,并且一次性取出来的个数为a(或b).当然,取出来的种类可能有很多种(即一个集合),问,这个集合中有多少个数字是相同的. 第一个输出集合长度为a的,第二个输出集合长度为b的,第三个输出无论如何都无法被取出的个数. 思路:建立正向图和反向图. 定义pair<int, int> interval[i] 表示第i

Gym 101246D Fire in the Country(dfs求SG函数)

http://codeforces.com/gym/101246/problem/D 题意: 给定一个无向有环图,大火从1点开始,每个时间点与它相邻的点也将会着火,现在有两个人轮流操作机器人,机器人从1点出发,每个人每次选择一个点走,谁最后被火烧了谁就输了. 思路: 博弈题. 我们先预处理求出每个点着火的时间点,然后根据时间点重建新图,也就是重新建一个有向无环图,原来图中相连的并且时间点相差1的相连,由时间低的连向时间高的. 接下来我们在新图上求每个点的SG值,SG值为0的点就是叶子结点,这样父

Gym 100463D Evil DFS

Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description Richard is evil. He wants to give another geometry problem to the participants of this contest and I’m afraid I have no choice but to comply. Wh