Caocao's Bridges HDU - 4738

Caocao‘s Bridges

HDU - 4738

题意:一个带权无向图,破坏一条边的代价是权重,如果可以破坏一条边使得存在两点不能互达,输出最小代价。

【找权值最小的桥】

找到桥更新答案即可。

注意如果本身图就不连通,则不需要破坏,代价为0

如果图不存在桥,则不满足输出-1

如果最后求出代价为0,则要输出1(因为题意是破坏桥,即使没有人守卫也要派一个人去炸桥)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int inf=0x3f3f3f3f;
 4 const int maxv=1000;
 5 int n,m;
 6 int ans;
 7 struct Edge
 8 {
 9     int v,w,nex;
10 }e[maxv*maxv<<1];
11 int head[maxv];
12 int cnt=0;
13 void init()
14 {
15     memset(head,-1,sizeof(head));
16     cnt=0;
17
18     ans=inf;
19 }
20 void add(int u,int v,int w)
21 {
22     e[cnt].v=v;
23     e[cnt].w=w;
24     e[cnt].nex=head[u];
25     head[u]=cnt++;
26 }
27
28 int pre[maxv],low[maxv],dfsk,bct;
29
30 void Tarjin(int u,int id)
31 {
32     pre[u]=low[u]=++dfsk;
33     for(int i=head[u];i!=-1;i=e[i].nex)
34     {
35         int v=e[i].v;
36         if(i==(id^1)) continue;  //反向边
37         if(!pre[v])
38         {
39             Tarjin(v,i);
40             low[u]=min(low[u],low[v]);
41             if(low[v]>pre[u]&&ans>e[i].w) ans=e[i].w;
42         }
43         else low[u]=min(low[u],pre[v]);
44     }
45 }
46
47 int main()
48 {
49     while(scanf("%d%d",&n,&m)&&(n||m))
50     {
51         init();
52         int u,v,w;
53         for(int i=0;i<m;i++)
54         {
55             scanf("%d%d%d",&u,&v,&w);
56             u--;v--;
57             add(u,v,w);
58             add(v,u,w);
59         }
60         memset(pre,0,sizeof(pre));
61         memset(low,0,sizeof(low));
62         dfsk=bct=0;
63         for(int i=0;i<n;i++) if(!pre[i])
64             bct++,Tarjin(0,-1);
65         if(bct>1) ans=0;
66         else if(ans==inf) ans=-1;
67         else if(ans==0) ans=1;
68         printf("%d\n",ans);
69
70
71     }
72
73 }

不用low数组,用dfs返回low值慢一些

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int inf=0x3f3f3f3f;
 4 const int maxv=1000;
 5 int n,m;
 6 int ans;
 7 struct Edge
 8 {
 9     int v,w,nex;
10 }e[maxv*maxv<<1];
11 int head[maxv];
12 int cnt=0;
13 void init()
14 {
15     memset(head,-1,sizeof(head));
16     cnt=0;
17
18     ans=inf;
19 }
20 void add(int u,int v,int w)
21 {
22     e[cnt].v=v;
23     e[cnt].w=w;
24     e[cnt].nex=head[u];
25     head[u]=cnt++;
26 }
27
28 int pre[maxv],dfsk,bct;
29
30 int Tarjin(int u,int id)
31 {
32     int lowu=pre[u]=++dfsk;
33     for(int i=head[u];i!=-1;i=e[i].nex)
34     {
35         int v=e[i].v;
36         if(i==(id^1)) continue;  //反向边
37         if(!pre[v])
38         {
39             int lowv=Tarjin(v,i);
40             lowu=min(lowu,lowv);
41             if(lowv>pre[u]&&ans>e[i].w) ans=e[i].w;
42         }
43         else lowu=min(lowu,pre[v]);
44     }
45     return lowu;
46 }
47
48 int main()
49 {
50     while(scanf("%d%d",&n,&m)&&(n||m))
51     {
52         init();
53         int u,v,w;
54         for(int i=0;i<m;i++)
55         {
56             scanf("%d%d%d",&u,&v,&w);
57             u--;v--;
58             add(u,v,w);
59             add(v,u,w);
60         }
61         memset(pre,0,sizeof(pre));
62         dfsk=bct=0;
63         for(int i=0;i<n;i++) if(!pre[i])
64             bct++,Tarjin(0,-1);
65         if(bct>1) ans=0;
66         else if(ans==inf) ans=-1;
67         else if(ans==0) ans=1;
68         printf("%d\n",ans);
69     }
70 }

Caocao's Bridges HDU - 4738

时间: 2024-07-30 16:15:28

Caocao's Bridges HDU - 4738的相关文章

(连通图 Tarjan)Caocao&#39;s Bridges --HDU --4738

链接: http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有很多岛屿,然后呢需要建造一些桥梁将所有的岛屿链接起来,周瑜要做的是就是不让曹操将所有岛屿连接起来,每个座桥有人在守卫, 周瑜只能炸一座桥,并且他派人去炸桥只能派的人数必须 大于等于守桥的人数.输出最小的炸桥人数, 要是没有答案就输出 -1 代码: #include <iostream> #include <cstdio> #include <cmath>

I - Caocao&#39;s Bridges - hdu 4738(求桥)

题意:曹操的船之间有一些桥连接,现在周瑜想把这些连接的船分成两部分,不过他只能炸毁一座桥,并且每座桥上有士兵看守,问,他最少需要排多少士兵去炸桥如果不能做到,输出‘-1’ 注意:此题有好几个坑,第一个输入桥守卫是0的话也得排一个士兵 如果一开始桥就是不连通的就不用派士兵了,直接输出 0 ************************************************************** #include<stdio.h>#include<string.h>#

Caocao&#39;s Bridges HDU - 4738 找桥

题意: 曹操在赤壁之战中被诸葛亮和周瑜打败.但他不会放弃.曹操的军队还是不擅长打水仗,所以他想出了另一个主意.他在长江上建造了许多岛屿,在这些岛屿的基础上,曹操的军队可以轻易地攻击周瑜的军队.曹操还修建了连接岛屿的桥梁.如果所有的岛屿都用桥连接起来,曹操的军队就可以很方便地部署在这些岛屿之间.周瑜无法忍受,他想毁掉曹操的一些桥梁,把一个或多个岛屿与其他岛屿分开.周瑜身上只有一颗炸弹,是诸葛亮留下的,所以他只能毁掉一座桥.周瑜必须派人带着炸弹去炸毁那座桥.桥上可能有警卫.轰炸队的士兵人数不能少于一

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

HDU 4738 Caocao&#39;s Bridges(找割边)

HDU 4738 Caocao's Bridges 题目链接 注意几个坑,可能重边,至少要派一个人去炸,没有连通的时候就不用炸了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 1005; const int INF = 0x3f3f3f3f; int pre[N], low[N

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

hdu 4738 Caocao&#39;s Bridges tarjan

Caocao's Bridges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and