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=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************************************************************
ll t,n,m,head[],vis[],vd[];
ll ans,sum;
struct ss
{
ll to,next;
ll w;
}e[];
void init()
{
t=;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
memset(vd,,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=;i<=n;i++)if(!vis[i])return;
ans=min(sum,ans);
}
void dfs(ll x)
{
if(x==)
{
boo();
}
for(ll i=head[x];i;i=e[i].next)
{
if(!vd[i])
{
if(i%)vd[i+]=;else vd[i-]=;
int bb=vis[e[i].to];
vis[e[i].to]=;
vd[i]=;
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]=;
if(i%)vd[i+]=;else vd[i-]=;
}
}
}
int main()
{
ll oo=;
while(scanf("%I64d%I64d",&n,&m)!=EOF)
{
ll a,b,c;
if(n==&&m==)break;
init();
for(ll i=;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=;
dfs(1ll);
if(n==||n==)ans=inf;
if(m==)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 ;
}

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

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

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

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

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  3. 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. ...

  4. Codeforces Gym 100286B Blind Walk DFS

    Problem B. Blind WalkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/cont ...

  5. Gym - 101480K_K - Kernel Knights (DFS)

    题意:有两队骑士各n人,每位骑士会挑战对方队伍的某一个位骑士. (可能相同) 要求找以一个区间s: 集合S中的骑士不会互相挑战. 每个集合外的骑士必定会被集合S内的某个骑士挑战. 题解:讲真被题目绕懵 ...

  6. Gym 100463D Evil DFS

    Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...

  7. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  8. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  9. Codeforces Gym 100463D Evil DFS

    Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...

随机推荐

  1. VS2013 EMMET插件学习

    在VS2013中搜索EMMET插件,安装,重启IDE,即可使用. 最简单的一个用法示例: 在编辑器里输入:ul>li*5 按快捷键:CTRL+1 即可生成如下代码: <ul> < ...

  2. 导出Excel之Epplus使用教程3(图表设置)

    导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...

  3. 我所使用的一个通用的Makefile模板

    话不多说,请看: 我的项目有的目录结构有: dirls/ ├── include │   └── apue.h ├── lib │   ├── error.c │   ├── error.o │   ...

  4. 在表单(input)中id和name的区别

    但是name在以下用途是不能替代的: 1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多name会同时对应多个控件,比如checkbox和radio,而id必须是 ...

  5. 一张图解释Hadoop IPC

    基于hadoop2.6.2.... 一张图Server启动,Client访问..... RPC是IPC的一种,IPC还有另外一种LPC,相关请看参考中的3 使用hadoop ipc步骤: 1.定义RP ...

  6. cocos基础教程(5)数据结构介绍之cocos2d::Map<K,V>

    1.概述 cocos2d::Map<K,V> 是一个内部使用了 std::unordered_map的关联容器模版. std::unordered_map 是一个存储了由key-value ...

  7. Item 表单页面的 Select2 相关业务逻辑

    Select2 插件官网:https://select2.github.io/ Select2 初始化说明: 代码在 public/javascripts/subchannel_items.js 中的 ...

  8. cocos2d::Vector

    C++中的vector使用范例 一.概述 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector是一个容器,它能够存放各种类型的对象,简 ...

  9. MYSQL注入天书之HTTP头部介绍

    Background-5 HTTP头部介绍 在利用抓包工具进行抓包的时候,我们能看到很多的项,下面详细讲解每一项. HTTP头部详解 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* ...

  10. LNK2005 连接错误解决办法

    nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@ ...