HDU 4005 The war Tarjan+dp
The war
1 2 1
2 3 2
4 3
1 2 1
1 3 2
1 4 3
3
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.
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e4+, M = 1e5, mod = 1e9+, inf = 2e9; int ans,scc,t,top,tot,head[N],n,m,dp[N][],a[N],b[N],c[N];
struct edge{int id,to,next,value;}e[M]; void add(int u,int v,int w) {e[t].next = head[u];e[t].to=v;e[t].value=w;e[t].id=;head[u]=t++;} int dfn[N],q[N],inq[N],low[N],belong[N],hav[N]; vector<pair<int ,int > > G[N];
void init() {
for(int i = ; i <= n; ++i) dp[i][] = dp[i][] = inf;
for(int i = ; i <= n; ++i) G[i].clear();
memset(hav,,sizeof(hav));
memset(dfn,,sizeof(dfn));
memset(head,-,sizeof(head));
t = tot = top = scc = ;
}
void dfs(int u) {
low[u] = dfn[u] = ++tot;
q[++top] = u; inq[u] = ;
for(int i = head[u]; i!=-; i = e[i].next) {
int to = e[i].to;
if(e[i].id) continue;
e[i].id = e[i ^ ].id = ;
if(!dfn[to]) {
dfs(to);
low[u] = min(low[u],low[to]);
} else if(inq[to]) low[u] = min(low[u],dfn[to]);
}
if(low[u] == dfn[u]) {
scc++;
do{
inq[q[top]] = ;
belong[q[top]] = scc;
}while(u != q[top--]);
}
}
void dfs_ans(int u,int fa) {
if(u == -) return ;
int fi = ;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i].first;
int value = G[u][i].second;
if(to == fa) continue;
dfs_ans(to,u);
if(!fi) {
dp[u][] = min(value,dp[to][]);
dp[u][] = dp[to][];
fi = ;
} else {
if(min(value,dp[to][]) < dp[u][]) dp[u][] = min(dp[u][],min(dp[to][],dp[u][])),dp[u][] = min(value,dp[to][]);
else dp[u][] = min(dp[u][],min(value,dp[to][]));
}
}
}
void Tarjan() {
int mi = inf, s = -, t = -;
for(int i = ; i <= n; ++i) if(!dfn[i]) dfs(i);
for(int i = ; i <= m; ++i) {
int fx = belong[a[i]];
int fy = belong[b[i]];
if(fx != fy) {
G[fx].push_back(MP(fy,c[i]));
G[fy].push_back(MP(fx,c[i]));
// cout<<fx<<" "<<fy<<endl;
if(c[i] < mi) {
s = fx,t = fy;
mi = c[i];
}
}
}
ans = inf;
dfs_ans(s,t);
dfs_ans(t,s);
if(s != - && t != -)ans = min(dp[s][],dp[t][]);
if(ans == inf) printf("%d\n",-);
else printf("%d\n",ans);
}
int main() {
while(~scanf("%d%d",&n,&m)) {
init();
for(int i = ; i <= m; ++i) {
scanf("%d%d%d",&a[i],&b[i],&c[i]);
add(a[i],b[i],c[i]);add(b[i],a[i],c[i]);
}
Tarjan();
}
return ;
}
HDU 4005 The war Tarjan+dp的更多相关文章
- HDU 4005 The war(双连通好题)
HDU 4005 The war pid=4005" target="_blank" style="">题目链接 题意:给一个连通的无向图.每条 ...
- HDU 4005 The war (图论-tarjan)
The war Problem Description In the war, the intelligence about the enemy is very important. Now, our ...
- hdu 4005 The war
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 In the war, the intelligence about the enemy is ...
- HDU 4005 The war(边双连通)
题意 给定一张 \(n\) 个点 \(m\) 条边的无向连通图,加入一条边,使得图中权值最小的桥权值最大,如果能使图中没有桥则输出 \(-1\). 思路 先对原图边双缩点,然后变成了一棵树.在 ...
- HDU 4005 The war 双连通分量 缩点
题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- BZOJ 1093: [ZJOI2007]最大半连通子图( tarjan + dp )
WA了好多次... 先tarjan缩点, 然后题意就是求DAG上的一条最长链. dp(u) = max{dp(v)} + totu, edge(u,v)存在. totu是scc(u)的结点数. 其实就 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
随机推荐
- STL 阅读(浅析)
写的不错,决定那这个看下.看的还是晕. http://luohongcheng.github.io/archives/
- C#之数据分页
方法一:临时datatable 创建临时表,临时变量 DataTable dt = null; //临时表 ; //总分页数 ; //当前页数 ; //每页的数量 加载数据到临时表,该方法测试放到了窗 ...
- Html5 postMessage
解释: 跨文档消息传输Cross Document Messaging. 编写代码前注意判断浏览器是否支持Html5 实例: b页面向a页面发送消息. <!DOCTYPE> <htm ...
- 服务器×××上的MSDTC不可用解决办法
MSDTC(分布式交易协调器),协调跨多个数据库.消息队列.文件系统等资源管理器的事务.该服务的进程名为Msdtc.exe,该进程调用系统Microsoft Personal Web Server和M ...
- 【python】An Introduction to Interactive Programming in Python(week two)
This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...
- 使用rdesktop连接Windows远程桌面
rdesktop 使用简单,windows也不和装什么服务端,是要把远程桌面共享打开就行了 安装 yum -y install rdesktop 具体使用方法要先打开终端,然后输入以下命令: rdes ...
- Sql如何自动定时备份数据库
直接上图
- 单击双击手势(UITapGestureRecognizer)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- 第K 小数
[问题描述]有两个正整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到N*M个数,询问这N*M个数中第K小数是多少.[输入格式]输入文件名为number.in.输入文件包 ...
- python基础——类和实例
python基础——类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都 ...