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. whereis命令

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...

  2. winscp私钥如何生成

    问题1,我用SecureCRT 5.0的自带工具生成了密钥和公钥(分别是不带后缀名的密钥文件和.pub的公钥文件),传上服务器也可以正常使用.     但是我用其它一些客户端工具连接时需要的密钥文件是 ...

  3. 移动前端调式页面--weinre

    一:远程调式工具---weinre 阅读目录 一:远程调式工具---weinre 二: 安装weinre 三: 访问weinre及在页面上调用 四:多用户 回到顶部 一:远程调式工具---weinre ...

  4. 那些不是秘密的微信earning方法

    微信这个新兴的移动平台着实培养了一些自媒体大号,让个人也能成为媒体中心,当然微信也成为了集富利器.他们是怎么做到的呢?让我们探究一下微信earn的方法吧,一起发散思维. ★微信公众平台推广功能公测,大 ...

  5. Radar Installation(贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56826   Accepted: 12 ...

  6. 关于ruby重构的过程中去除不必要的format

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) #这段话可以由下面的话替代56     respond_to do |format|57       ...

  7. [BZOJ1177][Apio2009]Oil

    [BZOJ1177][Apio2009]Oil 试题描述 采油区域 Siruseri政府决定将石油资源丰富的Navalur省的土地拍卖给私人承包商以建立油井.被拍卖的整块土地为一个矩形区域,被划分为M ...

  8. MySql 插入数据中文乱码

    在数据库连接URL后加上characterEncoding=UTF-8 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssm ...

  9. Python: 什么是*args和**kwargs

    转自: http://blog.sina.com.cn/s/blog_65a8ab5d0101fglm.html http://blog.csdn.net/chenjinyu_tang/article ...

  10. 【转】如何修改Chrome缓存目录的地址

    本文转自:http://www.nowamagic.net/librarys/veda/detail/2573 C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了A ...