zoukankan      html  css  js  c++  java
  • Caocao's Bridges HDU

    题目描述

    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 based on those islands, Caocao's army could easily attack Zhou Yu's troop. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao's army could be deployed very conveniently among those islands. Zhou Yu couldn't stand with that, so he wanted to destroy some Caocao's bridges so one or more islands would be seperated from other islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might be guards on bridges. The soldier number of the bombing team couldn't be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.

    输入格式

    There are no more than 12 test cases.

    In each test case:

    The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N 2 )

    Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )

    The input ends with N = 0 and M = 0.

    输出格式

    For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn't succeed any way, print -1 instead.

    样例

    Sample Input
    3 3
    1 2 7
    2 3 4
    3 1 4
    3 2
    1 2 7
    2 3 4
    0 0
    Sample Output
    -1
    4

    分析

    题意很简单,就是求图中最小桥的价值
    但要注意以下几点
    1、如果图不是联通的,输出0
    2、如果最小桥的值为0,不能输出0,要输出1,因为你至少要派一名士兵去
    3、有可能有重边

    代码

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int maxn=2000005;
    int head[maxn],tot=2;
    struct asd{
    	int val,to,next;
    }b[maxn];
    void ad(int aa,int bb,int cc){
    	b[tot].to=bb;
    	b[tot].val=cc;
    	b[tot].next=head[aa];
    	head[aa]=tot++;
    }
    int dfn[maxn],low[maxn],dfnc;
    bool bri[maxn];
    void tarjan(int now,int id){
    	dfn[now]=low[now]=++dfnc;
    	for(int i=head[now];i!=-1;i=b[i].next){
    		if(i==(id^1)) continue;
    		int u=b[i].to;
    		if(!dfn[u]){
    			tarjan(u,i);
    			low[now]=min(low[now],low[u]);
    			if(dfn[now]<low[u]){
    				bri[i]=bri[i^1]=1;
    			}
    		} else {
    			low[now]=min(low[now],dfn[u]);
    		}
    	}
    }
    int main(){
    	int n,m;
    	while(scanf("%d%d",&n,&m)!=EOF && n!=0){
    		memset(head,-1,sizeof(head));
    		memset(&b,0,sizeof(struct asd));
    		memset(dfn,0,sizeof(dfn));
    		memset(low,0,sizeof(low));
    		memset(bri,0,sizeof(bri));
    		dfnc=0;
    		tot=2;
    		for(int i=1;i<=m;i++){
    			int aa,bb,cc;
    			scanf("%d%d%d",&aa,&bb,&cc);
    			ad(aa,bb,cc),ad(bb,aa,cc);
    		}
    		tarjan(1,-1);
    		bool jud=0;
    		for(int i=1;i<=n;i++){
    			if(!dfn[i]){
    				jud=1;
    			}
    		}
    		if(jud){
    			printf("0
    ");
    			continue;
    		}
    		int ans=0x3f3f3f3f;
    		for(int i=2;i<tot;i++){
    			if(bri[i]==1){
    				ans=min(ans,b[i].val);
    			}
    		}
    		if(ans==0x3f3f3f3f) printf("-1
    ");
    		else printf("%d
    ",max(ans,1));
    	}
    	return 0;
    }
    
  • 相关阅读:
    144环形链表
    83. 删除排序链表中的重复元素
    21合并两个有序链表
    PyCharm2020激活破解教程
    Python正课目录
    2条pip命令解决Python项目依赖的导出和导出
    pip离线安装模块
    Python正课149 —— luffy项目 User表的配置
    Python正课148 —— luffy项目 数据库配置
    解决:django中LookupError No installed app with label 'admin'
  • 原文地址:https://www.cnblogs.com/liuchanglc/p/12812758.html
Copyright © 2011-2022 走看看