HDU4005 The war
HDU4005 The war
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 775 Accepted Submission(s): 128
Problem Description
In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situation of the enemy's war zones, and known that these war zones can communicate to each other directly or indirectly through the network. We also know the enemy is going to build a new communication line to strengthen their communication network. Our task is to destroy their communication network, so that some of their war zones can't communicate. Each line has its "cost of destroy". If we want to destroy a line, we must spend the "cost of destroy" of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they have already built, but we know nothing about the new line which our enemy is going to build. In this condition, your task is to find the minimum cost that no matter where our enemy builds the new line, you can destroy it using the fixed money. Please give the minimum cost. For efficiency, we can only destroy one communication line.
Input
The input contains several cases. For each cases, the first line contains two positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for the number of the enemy's war zones (numbered from 1 to n), and the number of lines that our enemy has already build. Then m lines follow. For each line there are three positive integer a, b, c (1<=a, b<=n, 1<=c<=100000), meaning between war zone A and war zone B there is a communication line with the "cost of destroy " c.
Output
For each case, if the task can be finished output the minimum cost, or output ‐1.
Sample Input
3 2
1 2 1
2 3 2
4 3
1 2 1
1 3 2
1 4 3
Sample Output
-1
3
Hint
For the second sample input: our enemy may build line 2 to 3, 2 to 4,
3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they
build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4,
we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can
destroy successfully, the minimum cost is 3.
*************************************************************************
题目大意:给定一个图,给这个图随机加上一条边,然后去破坏一条边使得图不连通,求保证能完成任务的最小花费。
用tarjan缩点形成一棵树,然后求出这个树中,每个节点的次小值儿子中的最小值。
这题的数据各种坑爹,我交了42次,看了别人的题解才勉强过,太坑爹啊。
#include <stdio.h> #include <algorithm> #include <vector> #include <string.h> #include <stack> #include <queue> #include <map> #define MIN(a,b) ((a)<(b)?(a):(b)) using namespace std; const int N=10010; const int E=200010; vector<vector< int > >gra[N],tu[N]; vector< int >temp; int fa[N],id,now,dfn[N],low[N],edge_up,vis[N],num; int gro[N],ans; struct Edge { int le,ri,val; bool operator<( const Edge &a) const { return val>a.val; } }edge[E]; priority_queue<Edge>que; stack< int >sta; void find( int s) { int t; low[s]=0x3f3f3f3f; for ( int i=0;i<tu[s].size();i++) if (!vis[t=tu[s][i][0]]) { vis[t]=1,find(t); low[t]=MIN(low[t],tu[s][i][1]); if (low[t]<low[s])ans=MIN(ans,low[s]),low[s]=low[t]; else ans=MIN(ans,low[t]); } } void tarjan( int s, int father) { fa[s]=father; dfn[s]=low[s]=++now; sta.push(s); for ( int i=0;i<gra[s].size();i++) { int e=gra[s][i][0]; if (!dfn[e]) { tarjan(e,s),low[s]=MIN(low[s],low[e]); if (low[e]>dfn[s]) { id++; while (1) { if (sta.empty()==1) break ; int t=sta.top(); gro[t]=id; sta.pop(); if (t==e) break ; } edge[edge_up].le=s; edge[edge_up].ri=e; edge[edge_up++].val=gra[s][i][1]; } } else if (e!=father) low[s]=MIN(low[s],dfn[e]); } } int main() { int n,m; while ( scanf ( "%d%d" ,&n,&m)==2) { while (sta.empty()==0)sta.pop(); memset (dfn,0, sizeof (dfn)); memset (low,0, sizeof (low)); memset (gro,0, sizeof (gro)); memset (fa,0, sizeof (fa)); memset (edge,0, sizeof (edge)); id=now=edge_up=0; for ( int i=0;i<=n;i++) gra[i].clear(),tu[i].clear(); for ( int i=1;i<=m;i++) { int a,b,v; scanf ( "%d%d%d" ,&a,&b,&v); temp.clear(); temp.push_back(b); temp.push_back(v); gra[a].push_back(temp); temp.clear(); temp.push_back(a); temp.push_back(v); gra[b].push_back(temp); } int ddd=0; for ( int i=1;i<=n;i++) { if (dfn[i]) continue ; ddd++; tarjan(i,0); id++; while (sta.empty()==0) { int t=sta.top(); gro[t]=id; sta.pop(); } } if (edge_up==0) { puts ( "-1" ); continue ; } while (que.empty()==0)que.pop(); for ( int i=0;i<edge_up;i++) { int a=edge[i].le=gro[edge[i].le]; int b=edge[i].ri=gro[edge[i].ri]; int v=edge[i].val; que.push(edge[i]); temp.clear(); temp.push_back(b); temp.push_back(v); tu[a].push_back(temp); temp.clear(); temp.push_back(a); temp.push_back(v); tu[b].push_back(temp); } memset (fa,-1, sizeof (fa)); int a,b,v; a=que.top().le; b=que.top().ri; v=que.top().val; que.pop(); num=0; //dfs(a,b);dfs(b,a); ans=0x3f3f3f3f; memset (vis,0, sizeof (vis)); vis[a]=vis[b]=1; find(a);find(b); printf ( "%d\n" ,ans<0x3f3f3f?ans:-1); } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法