CDOJ 889 Battle for Silver

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搜索。哎,好久未写搜索,当时居然脑残了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 510;
18 bool g[maxn][maxn],done[maxn];
19 int n,ans,p[maxn],w[maxn];
20 bool ok(int m,int x){
21     for(int i = 0; i < m; i++)
22         if(!g[p[i]][x]) return false;
23     return true;
24 }
25 void dfs(int m,int k,int sum){
26     ans = max(ans,sum);
27     if(m == 4) return;
28     for(int i = k+1; i <= n; i++){
29         if(!done[i] && ok(m,i)){
30             done[i] = true;
31             p[m] = i;
32             dfs(m+1,i,sum+w[i]);
33             done[i] = false;
34         }
35     }
36 }
37 int main() {
38     int i,m,u,v;
39     while(~scanf("%d %d",&n,&m)){
40         for(i = 1; i <= n; i++)
41             scanf("%d",w+i);
42             memset(g,false,sizeof(g));
43         for(i = 0; i < m; i++){
44             scanf("%d %d",&u,&v);
45             g[u][v] = g[v][u] = true;
46         }
47         ans = 0;
48         dfs(0,0,0);
49         printf("%d\n",ans);
50     }
51     return 0;
52 }

时间: 2024-11-09 03:52:43

CDOJ 889 Battle for Silver的相关文章

UESTC 889 Battle for Silver (dfs)

题意: 给一个图,每个点有点权,每两个点最多有一条边相连,每个点至少和一个点通过边相连. 要找出这样一个团,使得团内所有的点两两都有边相连且边不交叉,并且点权最大. 算法: 由于两两连边且边不能交叉,可知最多有4个点.所以暴搜~ dfs出4个位置放什么元素,一边判断放的点与前面的点是否是两两连边,一边更新ans. 开始一直当做3个点和4个点在写,忘了考虑1个点和2个点...WA了10次.. 自己写个样例自己推结果也是没考虑1个点和2个点的情况.. /* 6 7 1500 1000 100 200

UVAlive 6623 Battle for Silver(暴力+思路)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4634 思路:图中两条边之间不能交叉,则最终的选择的点集中的点只能有1,2,3,4个,暴力枚举即可. #include<cstdio> #include<cstring> #include<iostream> #include<

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

题意:给定一个图,让你找一个最大的子图,在这个子图中任何两点都有边相连,并且边不交叉,求这样子图中权值最大的是多少. 析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力就好,数据水,不会TLE的,才100多ms 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <

CDOJ 1220 The Battle of Guandu

The Battle of Guandu Time Limit: 6000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) In the year of 200, two generals whose names are Cao Cao and Shao Yuan are fighting in Guandu. The battle of Guandu was a great battle and the tw

CDOJ 1217 The Battle of Chibi

The Battle of Chibi Time Limit: 6000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is

CDOJ 32 树上战争(Battle on the tree) 解题报告

啊啊,最后一篇了,已经零点多了,我还打算写一段第一次打工赚钱做家教的感想呢…… 接下来有时间做决赛题,感觉也不是很难吼? 题目链接:http://acm.uestc.edu.cn/#/problem/show/32 很简单的题目,比较一棵有根树两个节点哪个高度. 同样,用了一行广搜.不要问我为什么叫一行广搜,不要让我压代码压得不成样子,编程可是一种艺术…… 也许我应该规范一下比如const变量的命名? #include <cstdio> #include <cstring> usi

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

CDOJ 1273 God Qing&#39;s circuital law

暴力枚举+idea.做的时候mod写错了,写成了1000000009,找了两个多小时才发现...... a[1],a[2],a[3]....a[N] b[1],b[2],b[3]....b[N] 首先需要枚举b[1]...b[N]与a[1]进行组合. 然后对a[2]...a[N]从小到大排序 对b[1],b[2],b[3]....b[N] 除当前与a[1]组合的以外,剩下的从大到小排序 然后找出每一个a[i]在不破坏a[0]最大值的情况下最大能与哪一个b[i]配对. 然后从第N个人开始往第2个人