POJ2735/Gym 100650E Reliable Nets dfs
2015-08-19 00:33
190 查看
[b]Problem E: Reliable Nets[/b]
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.
[b]Input[/b]
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.
[b]Output[/b]
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.
[b]Sample Input[/b]
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
[b]Sample Output[/b]
The minimal cost for test case 1 is 6.
There is no reliable net possible for test case 2.
[b]题意:[/b]
给你一个图,找出一个最小权和的经过所有点的环;
[b]题解:[/b]
数据小直接dfs找路,判断一下更新ans就好了
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.
[b]Input[/b]
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.
[b]Output[/b]
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.
[b]Sample Input[/b]
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
[b]Sample Output[/b]
The minimal cost for test case 1 is 6.
There is no reliable net possible for test case 2.
[b]题意:[/b]
给你一个图,找出一个最小权和的经过所有点的环;
[b]题解:[/b]
数据小直接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=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //************************************************************************************** ll t,n,m,head[11111],vis[11111],vd[11111]; ll ans,sum; struct ss { ll to,next; ll w; }e[300010]; void init() { t=1; memset(head,0,sizeof(head)); memset(vis,0,sizeof(vis)); memset(vd,0,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=1;i<=n;i++)if(!vis[i])return; ans=min(sum,ans); } void dfs(ll x) { if(x==1) { boo(); } for(ll i=head[x];i;i=e[i].next) { if(!vd[i]) { if(i%2)vd[i+1]=1;else vd[i-1]=1; int bb=vis[e[i].to]; vis[e[i].to]=1; vd[i]=1; 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]=0; if(i%2)vd[i+1]=0;else vd[i-1]=0; } } } int main() { ll oo=1; while(scanf("%I64d%I64d",&n,&m)!=EOF) { ll a,b,c; if(n==0&&m==0)break; init(); for(ll i=1;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=0; dfs(1ll); if(n==1||n==2)ans=inf; if(m==1)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 0; }
相关文章推荐
- HDU 1671 Trie树
- Linux第二天
- Python入门(十四) 字符串
- OpenGL绘制简单的参数曲线(一)——三次Hermite曲线
- BA-siemens-insight_ppcl_adapts函数用法
- VS2010编译器工具cl对c++11标准支持情况测试
- C++ Primer :第十章 :泛型算法之再探迭代器以及其他算法
- poj1151矩形面积并
- Intersecting Lines(直线相交判断,求相交交点)
- Foundation框架—字符串
- 【8-18】JS学习01
- Xcode常用快捷键
- Fair Division
- 【转】当你输入一个网址,实际会发生什么?
- 第88讲:Scala中使用For表达式实现map、flatMap、filter
- 计算身体质量指数
- 数据库:数据更新
- JVM内存结构
- HashMap HashTable CourrentHashMap 区别
- 2015.8.19 凌晨.. 博客开通 希望能从一个菜鸟慢慢变成大牛..