Battle for Silver

Time Limit: 2999/999MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others)

Piet Hein was a Dutch naval officer during the Eighty Years' War between the United Provinces of The Netherlands and Spain. His most famous victory was the capture of the Zilvervloot (Silver Fleet) near Cuba in 1628, where he intercepted a number of Spanish vessels that were carrying silver from the Spanish colonies in the Americas to Spain. Details about this famous naval battle are sketchy, so the description below may contain some historical inaccuracies.

The Silver Fleet consisted of vessels containing silver coins. Piet Hein's basic strategy was simple: tow away a number of vessels from the fleet, in order to capture their contents.

In an attempt to prevent the Dutch from carrying out this plan, the Spanish tied all the ships in their fleet together using huge iron chains. Each vessel in their fleet was fixed to at least one other vessel; any two vessels were connected by at most one chain; and the Spanish made sure that the chains did not cross each other, otherwise they could get tied up into a knot. As an end result, the vessels and the chains connecting them formed a connected, planar graph.

However, the Spanish preventive measures only made their situation worse. As an experienced naval officer, Piet Hein knew that towing away a group of ships was easiest if, for every two ships in the group, the ships were connected by a chain. He called such groupschaingroups.

Piet Hein ordered his men to tow away all the ships in the chaingroup that contained the largest amount of booty, after severing the links with the remaining ships in the Spanish fleet with a few highly accurate canon shots. The total booty in a chaingroup is the total number of silver coins in the vessels that make up the chaingroup.

The Silver Fleet represented as a graph: each dot denotes a vessel in the fleet, while each line denotes a chain that connects two vessels. The vessels that are connected in the figure by the dashed lines correspond to the chaingroup that provides the highest total value of silver coins. In this case, Piet Hein loots 4500 silver coins from the fleet.

Given a description of the Silver Fleet, find the value of the chaingroup with the highest amount of booty (i.e., total number of silver coins in the ships that make up the chaingroup).

Input

For each test-case:

  • A line containing two integers v (2≤v≤450) and e (1≤e≤900), the number of vessels in the fleet and the number of chains, respectively.
  • Then, v lines specifying S1,S2,…,Sv, the amount of silver coins carried by vessel i (1≤i≤v). The Si will be positive integers, where 100≤Si≤6000.
  • Then, for each chain, a line containing two integers cstart and cend, the two vessels connected by the chain, where (1≤cstart<cend≤v).

Each fleet forms a connected, planar graph.

Output

For each test case, one line containing a single positive integer: the number of silver coins that is captured by Piet Hein's fleet.

Sample Input

4 6
100
5000
1000
2000
1 2
1 3
1 4
2 3
2 4
3 4
6 8
1500
1000
100
2000
500
300
1 2
1 3
1 4
2 4
3 5
4 5
4 6
5 6

Sample Output

8100
4500
解题:dfs搜索。哎,好久未写搜索,当时居然脑残了。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
bool g[maxn][maxn],done[maxn];
int n,ans,p[maxn],w[maxn];
bool ok(int m,int x){
for(int i = ; i < m; i++)
if(!g[p[i]][x]) return false;
return true;
}
void dfs(int m,int k,int sum){
ans = max(ans,sum);
if(m == ) return;
for(int i = k+; i <= n; i++){
if(!done[i] && ok(m,i)){
done[i] = true;
p[m] = i;
dfs(m+,i,sum+w[i]);
done[i] = false;
}
}
}
int main() {
int i,m,u,v;
while(~scanf("%d %d",&n,&m)){
for(i = ; i <= n; i++)
scanf("%d",w+i);
memset(g,false,sizeof(g));
for(i = ; i < m; i++){
scanf("%d %d",&u,&v);
g[u][v] = g[v][u] = true;
}
ans = ;
dfs(,,);
printf("%d\n",ans);
}
return ;
}

CDOJ 889 Battle for Silver的更多相关文章

  1. UVaLive 6623 Battle for Silver (最大值,暴力)

    题意:给定一个图,让你找一个最大的子图,在这个子图中任何两点都有边相连,并且边不交叉,求这样子图中权值最大的是多少. 析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力 ...

  2. 2014 UESTC 暑前集训队内赛(3) 部分解题报告

    B.Battle for Silver 定理:完全图Kn是平面图当且仅当顶点数n<=4. 枚举所有完全图K1,K2,K3,K4,找出最大总权重. 代码: #include <iostrea ...

  3. CDOJ 1220 The Battle of Guandu

    The Battle of Guandu Time Limit: 6000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Oth ...

  4. CDOJ 1217 The Battle of Chibi

    The Battle of Chibi Time Limit: 6000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  5. cdoj 树上战争(Battle on the tree) Label:并查集?

    给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜. Input 输入包含多组数据 每组第 ...

  6. CDOJ UESTC 1220 The Battle of Guandu

    The 2015 China Collegiate Programming Contest 2015第一届中国大学生程序设计竞赛 F题 本质就是求单源最短路!注意会爆int 对于每一个村庄i,其实就是 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  9. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

随机推荐

  1. eclipse----快速设置主题色

  2. 使用js模拟ecshop元素挪移

    <!DOCTYPE html><html><head> <title>移动</title> <script src="jqu ...

  3. P3291 [SCOI2016]妖怪

    传送门 我数学的确白学了--这种题目竟然一点思路都没有-- 首先可以把每个妖怪看成二维平面上的一个点,那么每一个环境\((a,b)\)就可以看成一条斜率\(k=-\frac{b}{a}\)的过该点的直 ...

  4. display:none 和 hidden 区别

  5. blockhouses

    题意 : 给你一张图上面" X " 代表墙 , " . " 代表空地 , 让你在空地上放置炮台 , 条件是 不能 让彼此的炮台 可以互相看见 ( 隔着墙就看不见 ...

  6. 【LeetCode】-- 260. Single Number III

    问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...

  7. [转]android使用全局变量的两种方法

    本文转自:http://blog.csdn.net/feiyangxiaomi/article/details/9966215 在我们使用android编写程序的时候,少不了想利用全局变量,但是面向对 ...

  8. Android开发中常用的ListView列表的优化方式ViewHolder

    在Android开发中难免会遇到大量的数据加载到ListView中进行显示, 然后其中最重要的数据传递桥梁Adapter适配器是常用的,随着市场的需 求变化ListView'条目中的内容是越来越多这就 ...

  9. Android项目实战_手机安全卫士程序锁

    ###1.两个页面切换的实现1. 可以使用Fragment,调用FragmentTransaction的hide和show方法2. 可以使用两个布局,设置visibility的VISIABLE和INV ...

  10. 在Mac安装Scheme

    我也不知道安装的是Scheme这门语言还是编程环境还是其他的什么东西,反正能在控制台运行Scheme代码了.... 谁能告诉我怎么在sublime使用scheme.... 原地址 首先下载安装MIT- ...