http://pat.zju.edu.cn/contests/pat-a-practise/1018

在杭州各个点,有很多自助自行车的点,最大容纳点为CMAX,但比较适合的情况是CMAX/2,

现在从起点到终点,在最短路的情况下,调整各个车站的自行车数量使之CMAX/2,问开始时要提供的自行车最少与最后剩余的自行车最少的路径

最短路+DFS

在DFS时加上一个剪枝 if(lenth>dis[first])return ;就可以通过所有点

#include<stdio.h>
#define MAX 0x3ffffff
int cmax,N,end,M;
int node[];
int map[][];
int path[]; int rpath[];
int in,out,nowstep,rstep; bool hash[][];
int shortlen; int dis[];
bool use[];
int ok=; void dijk( )
{
int n=N,m;
int min,rj,i,j;
for(j=;j<=n;j++)
{
dis[j]=map[][j];
use[j]=;
}
use[]=;
for(i=;i<n;i++)
{
min=MAX;
for(j=;j<=n;j++)
{
if(use[j]==)continue;
if(dis[j]<min)
{
min=dis[j];
rj=j;
}
} use[rj]=;
for(j=;j<=n;j++)
{
if(use[j]==)continue;
if(dis[j]>dis[rj]+map[rj][j])
dis[j]=dis[rj]+map[rj][j];
}
} shortlen=dis[end];
} void print(){
int i;
printf("%d",in);
printf("");
for(i=;i<=rstep;i++){
printf("->%d",rpath[i]);
}
printf(" %d\n",out);
} void cal(){
int tin=,tout=;
int i,add=;
for(i=;i<=nowstep;i++){
add+=node[path[i]]-cmax/;
if(add<tin)tin=add;
}
if(tin>)tin=;
else tin=-tin;
tout=add+tin; if((tin<in)||((tin==in)&&tout<out)){
in=tin;
out=tout;
if(in==&&tout==)ok=;
rstep=nowstep;
for(i=;i<=nowstep;i++){
rpath[i]=path[i];
}
} //test
/*
printf("%d",tin);
printf(" 0");
for(i=1;i<=nowstep;i++){
printf("->%d",path[i]);
}
printf(" %d\n",tout);
*/
}
void dfs(int step,int first,int lenth)
{
if(ok==||lenth>dis[first])return ; //lenth>dis[first] 是个关键的剪枝,不加的话最后一个点会超时
if(lenth==shortlen&&first==end){
nowstep=step;
cal();
} int j;
for(j=;j<=N;j++){
if(hash[first][j]==||map[j][first]==MAX)continue;
hash[first][j]=hash[j][first]=;
path[step+]=j;
dfs(step+,j,lenth+map[first][j]);
hash[first][j]=hash[j][first]=;
}
} int main()
{
while(scanf("%d%d%d%d",&cmax,&N,&end,&M)!=EOF){
int i,j,temp;
ok=;
in=;
out=; for(i=;i<=N;i++){
for(j=;j<=N;j++){
map[i][j]=MAX;
hash[i][j]=;
}
} for(i=;i<=N;i++){
scanf("%d",&node[i]);
} int ll,rr,v;
for(i=;i<=M;i++){
scanf("%d%d%d",&ll,&rr,&v);
map[ll][rr]=map[rr][ll]=v;
}
dijk();
dfs(,,);
print();
} return ;
}

PAT1018——最短路加DFS的更多相关文章

  1. NOIP 2013 货车运输 最大生成树加DFS巧妙AC

    #include<set> #include<map> #include<cmath> #include<queue> #include<stac ...

  2. HDU(1572),最短路,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...

  3. poj3160强连通分量加dfs

    After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such ...

  4. Treasure Hunt--poj1066(最短路加判断线段的关系)

    http://poj.org/problem?id=1066 题目大意:有n条线段 他们都在这个房间里   最后有一个点代表起始位置 现在想通过墙出去  他只能爆破每个房间的中点的门   问最少的门通 ...

  5. Labyrinth 树的直径加DFS

    The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...

  6. HDU - 2066 最短路+加一个节点

    一个图上,有M条边,Z个出发点,Y个终止点.求一条最短路,其中起点是Z中的任意一点,终点是Y中任意一点. Input 输入数据有多组,输入直到文件结束. 每组的第一行是三个整数M,Z,Y 接着有M行, ...

  7. nyoj 203 三国志(最短路加01背包)

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下, ...

  8. zoj 2110 很好的dfs+奇偶剪枝

    //我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的 //剪纸奇偶剪枝加dfs #include<stdio.h> #include<queue> ...

  9. hdu 6852Path6(最短路+最小割)

    传送门 •题意 有n个城市,标号1-n 现花费最小的代价堵路 使得从1号城市到n号城市的路径边长 (注意只是变长不是最长) 堵一条路的代价是这条路的权值 •思路 在堵路以前,从1到n的最小路径当然是最 ...

随机推荐

  1. maven说明

    1.maven 仓库地址 http://mvnrepository.com/ 2.maven jar包搜索地址 http://search.maven.org/ 3. 点开上面的 版本链接,就可以看到 ...

  2. The remote end hung up unexpectedly while git cloning

    https://stackoverflow.com/questions/6842687/the-remote-end-hung-up-unexpectedly-while-git-cloning Qu ...

  3. Android -- SQLite 数据库创建,增删改查,事务处理

    1. 概述 在Android平台上,集成了一个嵌入式关系型数据库-SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进制对象)数据类 ...

  4. MarkDown中锚的使用

    经常使用Markdown配合Mou编辑器来些点总结和文档,但是其中需要前后引用或链接时markdown木有提供直接的方式.当然,Markdown支持嵌入式HTML语法,so..实现起来也没啥问题. 具 ...

  5. 报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMapper is not known to the MapperRegistry.

    报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMa ...

  6. ActiveMQ 性能调优

    本章重点 学习普通的性能调优技巧 怎么优化生产者和消费者 调优实例 简介 ActiveMQ 的性能高度依赖于多种不同的因素,包括:网络代理拓扑架构,传输层,底层网络的服务质量和速度,硬件,操作系统和 ...

  7. mysql中的左连接右连接内连接

    一. 初始化SQL语句 /*join 建表语句*/ drop database if exists test; create database test; use test; /* 左表t1*/ dr ...

  8. Struts10---拦截器

    01.创建一个登录界面 <%@ page language="java" import="java.util.*" pageEncoding=" ...

  9. JavaScrip练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. react antd layout sider

    import React from 'react'; import {Link, withRouter} from 'react-router-dom'; import {Layout, Menu, ...