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 ...
随机推荐
- svn代码回滚命令
代码回滚提交: 比如要把73回滚到68 svn merge -r 73:68 http://my.repository.com/my/project/trunk 然后commit就行了 svn com ...
- WPF 检测输入状态
[DllImport("user32.dll")] static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); pub ...
- 新浪微博客户端(11)-自定义checkBox
在最后一个欢迎界面上添加一个CheckBox. // 2.添加4个UIImageView ; i < NEW_FEATURE_NUMS; i++) { UIImageView *imageVie ...
- C++ 中宏的使用 --来自:http://blog.csdn.net/hgl868/article/details/7058906
宏在代码中的使用实例: g_RunLog2("Middleware client for Linux, build:%s %s", __DATE__, __TIME__); 下面详 ...
- logback 项目应用
1.gradle引用: compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3' compile grou ...
- [Effective JavaScript 笔记] 第4条:原始类型优于封闭对象
js有5种原始值类型:布尔值.数字.字符串.null和undefined. 用typeof检测一下: typeof true; //"boolean" typeof 2; //&q ...
- iATKOS v7硬盘安装教程(硬盘助手+变色龙安装版)
这是作者:Tong 写的一篇安装教程 首先感谢:wowpc制作的变色龙安装版.iATKOS作者以及硬盘安装助手作者 前言:现在时代在进步,系统同样也在进步,在以前要在PC上整个Mac是很痛苦的事情,就 ...
- Inheritance
Often, classes will have shared characteristics with other classes. Rewriting the same methods for e ...
- compact过滤数组中的nil
http://ruby-doc.org/core-2.2.0/Array.html#method-i-compact compact → new_aryclick to toggle source R ...
- Leetcode 之Convert Sorted Array to Binary Search Tree(54)
思路很简单,用二分法,每次选中间的点作为根结点,用左.右结点递归. TreeNode* sortedArrayToBST(vector<int> &num) { return so ...