ICPC Asia Regional 2015 Japan.Routing a Marathon Race(DFS)
\(Description\)
给定一张\(n\)个点\(m\)条边的无向图,每个点有一个权值。求一条从\(1\)到\(n\)的路径,使得代价最小,输出最小代价。
一条路径的代价定义为,路径上所有点以及和这些点相邻的所有点的权值和。
\(n\leq40,\ m\leq\frac{n(n-1)}{2}\)。
\(Solution\)
容易发现,如果选择从\(u\)走到\(v\),那么一定不会再回到\(u\)的其它相邻节点(不如直接走过去)。
这样从点\(u\)爆搜的话,每次移动都会删掉\(dgr_u\)个点。
那么复杂度是:\(T(n)=c\times T(n-c)\),最差情况下是\(c=3\),复杂度约是\(3^{\frac n3}\)。
注意边数是\(N\times N\)啊不除\(2\)!!!(mdzz)
//1ms 2704kb
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=45,M=N*N;
int n,Ans,Enum,H[N],nxt[M],to[M],A[N],ban[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
inline void AE(int u,int v)
{
to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
to[++Enum]=u, nxt[Enum]=H[v], H[v]=Enum;
}
void DFS(int x,int cost)
{
if(x==n)
{
for(int i=H[x]; i; i=nxt[i]) !ban[to[i]]&&(cost+=A[to[i]]);//!
Ans=std::min(Ans,cost);
return;
}
for(int i=H[x]; i; i=nxt[i]) !ban[to[i]]&&(cost+=A[to[i]]), ++ban[to[i]];
for(int i=H[x]; i; i=nxt[i]) if(ban[to[i]]==1) DFS(to[i],cost);
for(int i=H[x]; i; i=nxt[i]) --ban[to[i]];
}
int main()
{
int n=read(),m=read();
for(int i=1; i<=n; ++i) A[i]=read();
for(int i=1; i<=m; ++i) AE(read(),read());
::n=n, Ans=2e9, ban[1]=1, DFS(1,A[1]), printf("%d\n",Ans);
return 0;
}
ICPC Asia Regional 2015 Japan.Routing a Marathon Race(DFS)的更多相关文章
- hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online
Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...
- (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )
http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others) Memo ...
- (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)
http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others) ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 1310 ...
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
- hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...
- hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
随机推荐
- mysql入门练习
2.详细解释列mysql执行语句的每个参数与参数值的含义 mysql -hlocalhost -P3306 -uroot -proot 连接数据库,端口号为3306, 用户名root, 密码roo ...
- SVN项目迁移到Git上(并带有完整的提交记录)
公司需求:早期的一些项目使用的是SVN,现在想要更换为Git,需要代码迁移并且能在Git上看到之前在SVN中的项目的提交记录,公司没有使用gitlab,代码都push在公司的服务器上,用的是Torto ...
- 创建WIFI热点
@echo off:beginecho 笔记本做无线WiFi程序(首次使用请先设置WiFi帐户.)echo 1.设置WiFi帐户,请按1echo 2.开启WiFi功能,请按2echo 3.闭关WiFi ...
- 类和JSP关系
404的原因.除了路径问题,还有文件放置位置.比如如果文件在web-inf下面.浏览器是访问不到的
- IDEA窗口重置
- Spring Boot学习--项目启动时执行指定service的指定方法
Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner. 这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方 ...
- VUE,页面跳转传多个参数
<template> <a @click="goNext">Next</a> <input type="text" v ...
- SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问
SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问 https://blog.csdn.net/yft_android/article/details/80307672
- Processing3 1.随机行走
class Walker { int x; int y; Walker() { x = width/2; y = height/2; } void display() { stroke(0); poi ...
- Linux系统监控命令及定位Java线程
1.PID.TID的区分 uid是user id,即用户id,root用户的uid是0,0为最高权限,gid是group id,用户组id,使用 id 命令可以很简单的通过用户名查看UID.GID:~ ...