【BZOJ】3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆(tarjan)
http://www.lydsy.com/JudgeOnline/problem.php?id=3397
显然先tarjan缩点,然后从枚举每一个scc,然后向其它岛屿连费用最小的边,然后算最小的即可
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=505;
int ihead[N], mn[N][N], cnt, FF[N], LL[N], vis[N], tot, q[N], top, p[N], scc, n;
struct ED { int to, next; }e[N*N<<1];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
}
void tarjan(int u) {
int v;
FF[u]=LL[u]=++tot; q[++top]=u; vis[u]=1;
for(int i=ihead[u]; i; i=e[i].next) {
v=e[i].to;
if(!FF[v]) tarjan(v), LL[u]=min(LL[u], LL[v]);
else if(vis[v]) LL[u]=min(LL[u], FF[v]);
}
if(FF[u]==LL[u]) {
++scc;
do {
v=q[top--];
p[v]=scc;
vis[v]=0;
} while(u!=v);
}
} int main() {
read(n);
for1(i, 1, n) {
int u=getint(), v=getint();
add(u, v);
}
for1(i, 1, n) if(!FF[i]) tarjan(i);
CC(mn, 0x3f);
for1(i, 1, n) for1(j, 1, n) {
int t=getint(), u=p[i], v=p[j];
if(u!=v && mn[u][v]>t) mn[u][v]=t;
}
int ans=~0u>>1;
for1(i, 1, scc) {
int t=0;
for1(j, 1, scc) if(i!=j)
t+=mn[i][j];
ans=min(t, ans);
}
print(ans<<1);
return 0;
}
Description
Input
Output
Sample Input
1 7
7 3
3 6
6 10
10 1
2 12
2 9
8 9
8 12
11 5
5 4
11 4
0 15 9 20 25 8 10 13 17 8 8 7
15 0 12 12 10 10 8 15 15 8 8 9
9 12 0 25 20 18 16 14 13 7 12 12
20 12 25 0 8 13 14 15 15 10 10 10
25 10 20 8 0 16 20 18 17 18 9 11
8 10 18 13 16 0 10 9 11 10 8 12
10 8 16 14 20 10 0 18 20 6 16 15
13 15 14 15 18 9 18 0 5 12 12 13
17 15 13 15 17 11 20 5 0 22 8 10
8 8 7 10 18 10 6 12 22 0 11 12
8 8 12 10 9 8 16 12 8 11 0 9
7 9 12 10 11 12 15 13 10 12 9 0
Sample Output
HINT

Source
【BZOJ】3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆(tarjan)的更多相关文章
- bzoj:3397 [Usaco2009 Feb]Surround the Islands 环岛篱笆
Description 约翰在加勒比海买下地产,准备在这里的若干个岛屿上养奶牛.所以,他要给所有岛屿围上篱笆.每个岛屿都是多边形.他沿着岛屿的一条边界朝一个方向走,有时候坐船到另一个岛去.他可 ...
- BZOJ3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆
3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 11 So ...
- Bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 dijkstra,堆,分层图
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1573 Solv ...
- BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级( 最短路 )
最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...
- BZOJ 1578: [Usaco2009 Feb]Stock Market 股票市场( 背包dp )
我们假设每天买完第二天就卖掉( 不卖出也可以看作是卖出后再买入 ), 这样就是变成了一个完全背包问题了, 股票价格为体积, 第二天的股票价格 - 今天股票价格为价值.... 然后就一天一天dp... ...
- BZOJ 3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛( dp )
水题...忘了取模就没1A了.... --------------------------------------------------------------------------- #incl ...
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 -- 分层图最短路
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MB Description 每天,农夫 ...
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 优先队列+dij
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1768 Solv ...
- bzoj:1723: [Usaco2009 Feb]The Leprechaun 寻宝
Description 你难以想象贝茜看到一只妖精在牧场出现时是多么的惊讶.她不是傻瓜,立即猛扑过去,用她那灵活的牛蹄抓住了那只妖精. “你可以许一个愿望,傻大个儿!”妖精说. “财富 ...
随机推荐
- MySQL删除表的时候忽略外键约束
删除表不是特别常用,特别是对于存在外键关联的表,删除更得小心.但是在开发过程中,发现Schema设计的有问题而且要删除现有的数据库中所有的表来重新创建也是常有的事情:另外在测试的时候,也有需要重新创建 ...
- DOM BOM document window 区别
DOM 是为了操作文档出现的 API,document 是其的一个对象: BOM 是为了操作浏览器出现的 API,window 是其的一个对象. 使用下图讲解: 归DOM管的: E区:即doc ...
- 用Java axis2调用.net平台的Webservice出现的一些问题
问题1: AxisFault faultCode: {http://schemas.microsoft.com/ws/2005/05/addressing/none}ActionNotSupporte ...
- Drupal如何集中控制静态变量?
Drupal许多的函数中都使用了静态变量.按照通常的用法,静态变量的使用应该是这样的: function drupal_set_title($title = NULL) { static $store ...
- IOS炫酷的引导界面
代码地址如下:http://www.demodashi.com/demo/11246.html 一.准备工作 1.先用时ps工具制作好图片 2.然后计算好每张图片通过滑动视图的偏移量来改变图片的位置 ...
- ThreadLocal源码
/* * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...
- Android webView 支持缩放及自适应屏幕
//支持javascript web.getSettings().setJavaScriptEnabled(true); // 设置可以支持缩放 web.getSettings().setSupp ...
- Vue Element Form表单时间验证控件使用
如果直接使用Element做时间选择器,其规则(rules)不添加type:'date',会提示类型错误,处理这个需要规范值的类型为date. 时间格式化过滤器 import Vue from 'vu ...
- Android 混淆代码总结
为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的步骤: 1. 大家也许都注意到新建一个工程会看到项目下边有这样proguard-project.txt一个文件,这 ...
- How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse---转载
How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse 原文:http ...