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=,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的更多相关文章
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- 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. ...
- Codeforces Gym 100286B Blind Walk DFS
Problem B. Blind WalkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/cont ...
- Gym - 101480K_K - Kernel Knights (DFS)
题意:有两队骑士各n人,每位骑士会挑战对方队伍的某一个位骑士. (可能相同) 要求找以一个区间s: 集合S中的骑士不会互相挑战. 每个集合外的骑士必定会被集合S内的某个骑士挑战. 题解:讲真被题目绕懵 ...
- Gym 100463D Evil DFS
Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...
随机推荐
- DataGridView设置不自动显示数据库中未绑定的列
项目中将从数据库查出来的数据绑定到DataGridView,但是不想显示所有的字段.此功能可以通过sql语句控制查出来的字段数目,但是DataGridView有属性可以控制不显示未绑定的数据,从UI层 ...
- JavaScript 参数传递与变量复制
ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值. 基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 5 种基本数 ...
- C++ 的语言杂谈(一)--C++不是新手友好的
C++的语言品味是独特的,喜欢的人特别喜欢,讨厌的人特别讨厌.虽然Bjane Stroustrup不断地宣称C++的发展方向是新手友好的,但实际上对新手来说,最重要的还是有强大方便的标准库可以使用(像 ...
- lamp 网站打不开,不显示也不报错,
原因是该网站的编程员,习惯简写,<? ?>;而服务器版本的php.ini 默认不支持只支持<?php ?>这种格式. 解决方法vim /usr/loacl/php/etc/ph ...
- javascript将毫秒转换成hh:mm:ss的形式
function formatMilliseconds(value) { var second = parseInt(value) / 1000; // second var minute = 0; ...
- XSS 探索
1. 什么是XSS攻击? 正常的页面被渗出了攻击者的js脚本,这些脚本可以非法地获取用户信息,然后将信息发送到attacked的服务端. XSS是需要充分利用输出环境来构造攻击脚本的 2. 危害 非法 ...
- 自定义继承于Page的基类
自定义继承于Page的基类:MyBasePage[校验用户是否登录,如果登录则获取用户信息,否则跳转到登录页面]============================================ ...
- 关于showModalDialog()对话框点击按钮弹出新页面的问题
页面a.aspx上,单击按钮a,走脚本,弹出showModalDialog("b.aspx",....) 在b.aspx上有个服务器控件按钮b,单击按钮,更新数据后,会弹出一个新的 ...
- setVolumeControlStream(int streamType)
Android中有如下几种音频流: AudioManager.STREAM_MUSIC /** The audio stream for music playback */ AudioManager ...
- Python序列切片的注意事项
a=[1,2,3,4,5,6,7,8,9,10] 1)普通切片,形如array[m:n],只包含起始索引m,和不被包含在结果内的终点索引n, 注意终点索引可以大于序列的大小(长度),若终点索引大于序列 ...