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;
}
05-28
`Routes` 是 React Router v6 中的一个新组件,用于定义路由规则。与之前版本的 `Route` 组件不同,`Routes` 允许你同时定义多个路由规则,并支持嵌套路由。 `Routes` 组件的基本用法如下: ```jsx import { Routes, Route } from 'react-router-dom'; function MyRouter() { return ( <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="/users" element={<Users />}> <Route path="/" element={<UsersList />} /> <Route path=":id" element={<UserProfile />} /> </Route> <Route path="*" element={<NotFound />} /> </Routes> ); } ``` 在这个例子中,我们使用 `Routes` 组件来定义了四个路由规则: - `/`:当访问根路径时,展示 `Home` 组件。 - `/about`:当访问 `/about` 路径时,展示 `About` 组件。 - `/users`:当访问 `/users` 路径时,展示 `Users` 组件,并且该组件下有两个子路由规则: - `/`:当访问 `/users` 路径时,展示 `UsersList` 组件。 - `/:id`:当访问 `/users/:id` 路径时,展示 `UserProfile` 组件。 - `*`:当访问任何未定义的路径时,展示 `NotFound` 组件。 需要注意的是,如果你使用了嵌套路由,子路由的路径应该是相对于父路由的路径的。例如,在上面的例子中,`UsersList` 组件的路径实际上是 `/users`,而不是 `/users/`。 另外,`Routes` 组件是一个容器组件,因此你需要在它内部使用 `Route` 组件来定义具体的路由规则。每个 `Route` 组件都需要指定 `path` 属性和 `element` 属性,前者用于匹配当前 URL,后者用于展示对应的组件。同时,`Route` 组件还支持其他属性,例如 `exact`、`caseSensitive` 等等,用于进一步控制路由匹配的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值