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 ;
}
上一篇:ETL工具--kettle篇(17.10.09更新)


下一篇:利用结果集元数据将查询结果封装为map