Running Routes 题解

题目链接

Running Routes

题目描述

Running Routes

The administrators at Polygonal School want to increase enrollment, but they are unsure if their gym can support having more students. Unlike a normal, boring, rectangular gym, the gym floor at Polygonal is a regular n n n-sided polygon! They affectionately refer to the polygon as P P P.

The coach has drawn several running paths on the floor of the gym. Each running path is a straight line segment connecting two distinct vertices of P P P. During gym class, the coach assigns each student a different running path, and the student then runs back and forth along their assigned path throughout the class period. The coach does not want students to collide, so each student’s path must not intersect any other student’s path. Two paths intersect if they share a common point (including an endpoint).

Given a description of the running paths in P P P, compute the maximum number of students that can run in gym class simultaneously.

Figure 1: Illustrations of the two sample inputs, with possible solutions highlighted in thick red lines. Solid black lines represent running paths that are not assigned to a student, and dashed black lines are used to show the boundary of P P P in places where no running path exists.

Input

The first line contains an integer n n n ( 3 ≤ n ≤ 500 3 \le n \le 500 3n500), the number of vertices in P P P. (The vertices are numbered in increasing order around P P P.) Then follows n n n lines of n n n integers each, representing a n × n n \times n n×n symmetric binary matrix which we’ll call M M M. The j th j^{\text {th}} jth integer on the i th i^{\text {th}} ith line M i j M_{ij} Mij is 1 1 1 if a running path exists between vertices i i i and j j j of the polygon, and 0 0 0 otherwise. It is guaranteed that for all 1 ≤ i , j ≤ n 1 \le i,j \le n 1i,jn, M i j = M j i M_{ij} = M_{ji} Mij=Mji and M i i = 0 M_{ii}=0 Mii=0.

Output

Print the maximum number of students that can be assigned to run simultaneously on the running paths, given the above constraints.

Sample Input 1Sample Output 1
3
0 1 1
1 0 1
1 1 0
1



Sample Input 2Sample Output 2
6
0 0 0 1 0 0
0 0 0 0 1 1
0 0 0 0 1 1
1 0 0 0 0 0
0 1 1 0 0 0
0 1 1 0 0 0
2






题目大意

给定一个正 n n n边形,以及点与点之间能否连线的关系,现在要选出最多条连线,使得所有连线两两不相交,问最多能选出几条线。

解题思路

区间 d p dp dp

考虑正 n n n边形按编号排成一圈,一个区间 [ l , r ] [l,r] [l,r],假设 [ l , r ] [l,r] [l,r]中一个点 P P P向点 l l l连接,那么区间 [ l + 1 , P − 1 ] [l+1,P-1] [l+1,P1]里的点就不能向 [ P + 1 , r ] [P+1,r] [P+1,r]中的点连线了。由此可以设 f l , r f_{l,r} fl,r表示区间 [ l , r ] [l,r] [l,r]中最多能连几条线,那么状态转移方程是 f l , r = max ⁡ ( f l + 1 , P − 1 + f P + 1 , r + a l , P ) ( l ≤ P < r ) f_{l,r}=\max(f_{l+1,P-1}+f_{P+1,r}+a_{l,P})(l\leq P<r) fl,r=max(fl+1,P1+fP+1,r+al,P)(lP<r)

代码实现

#include<bits/stdc++.h>
#define For(i,a,b)for(int i=a;i<=b;i++)
using namespace std;
int n,len,a[600][600],f[600][600];
int main(){
	scanf("%d",&n);
	For(i,1,n)For(j,1,n)scanf("%d",&a[i][j]);
	For(t,1,n-1)For(i,1,n-t)For(k,i,i+t)f[i][i+t]=max(f[i][i+t],f[i+1][k-1]+f[k+1][i+t]+a[i][k]);
	return printf("%d\n",f[1][n]),0;
}
  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值