https://www.patest.cn/contests/pat-a-practise/1018

先用Dijkstra算出最短路,然后二分答案来验证,顺便求出剩余最小,然后再从终点dfs回去求出路径

#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e3 + 10;
int c, n, s, m, f[maxn], map[maxn][maxn], x, y, z, dis[maxn], vis[maxn], ans; void dfs(int x, int y)
{
if (y < 0) return;
if (x == s) { ans = min(ans, y); return; }
for (int i = 0; i <= n; i++)
{
if (map[x][i] == -1) continue;
if (dis[i] == dis[x] + map[x][i]) dfs(i, y - (c / 2 - f[i]));
}
} bool Dfs(int x, int y, int z)
{
if (!x) { if (y == z) { printf("0"); return true; } else return false; }
for (int i = 0; i <= n; i++)
{
if (map[x][i] == -1) continue;
if (dis[x] == dis[i] + map[x][i])
{
if (Dfs(i, y + c / 2 - f[x], z))
{
printf("->%d", x);
return true;
}
}
}
return false;
} int main()
{
scanf("%d%d%d%d", &c, &n, &s, &m);
for (int i = 1; i <= n; i++) scanf("%d", &f[i]);
memset(map, -1, sizeof(map));
memset(dis, -1, sizeof(dis));
while (m--)
{
scanf("%d%d%d", &x, &y, &z);
if (map[x][y] == -1 || map[x][y] > z) map[x][y] = map[y][x] = z;
}
dis[0] = 0;
while (true)
{
int now = -1;
for (int i = 0; i <= n; i++)
{
if (!vis[i] && dis[i] != -1 && (now == -1 || dis[i] < dis[now])) now = i;
}
if (now == -1) break;
vis[now] = 1;
for (int i = 0; i <= n; i++)
{
if (map[now][i] == -1) continue;
if (dis[i] == -1 || dis[i]>dis[now] + map[now][i]) dis[i] = dis[now] + map[now][i];
}
}
int q = 0, h = n*c / 2;
while (q <= h)
{
int mid = q + h >> 1;
ans = INF; dfs(0, mid);
if (ans != INF) h = mid - 1; else q = mid + 1;
}
printf("%d ", q);//需要运出的数量
dfs(0, q);
Dfs(s, ans, q);
printf(" %d\n", ans);//需要运回的数量
return 0;
}

PAT A 1018. Public Bike Management (30)【最短路径】的更多相关文章

  1. PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)

    1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides ...

  2. PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  3. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

  4. 1018 Public Bike Management (30)(30 分)

    时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...

  5. 1018 Public Bike Management (30 分)

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  6. 1018 Public Bike Management (30分) 思路分析 + 满分代码

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  7. 1018. Public Bike Management (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...

  8. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  9. PAT (Advanced Level) 1018. Public Bike Management (30)

    先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include&l ...

随机推荐

  1. python标准模块(三)

    本文会涉及到的模块: subprocess logging 1. subprocess 可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 p ...

  2. 解决Spring MVC @ResponseBody返回中文字符串乱码问题

    spring mvc使用的默认处理字符串编码为ISO-8859-1 解决方法: 第一种方法: 对于需要返回字符串的方法添加注解,如下: @RequestMapping(value="/use ...

  3. Google Maps API V3 之 图层

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  4. 【转】JavaWeb MVC

    -------------------------------------------------------------------------------------------------- 1 ...

  5. Microsoft SQL Server 2008 R2官方中文版(SQL2008下载).rar

    Microsoft SQL Server 2008 R2官方中文版(SQL2008下载).rar

  6. c++11 中成员变量初始化的顺序

    参考C++11FAQ https://www.chenlq.net/cpp11-faq-chs 11以后可以直接在类里面初始化成员变量,类似这样 class A { int a=1; const in ...

  7. ViewHolder的改进写法

    先看看ViewHolder通用写法         ViewHolder holder = null;         if(convertView == null){                 ...

  8. C和指针 第十三章 高级指针话题

    高级声明: int (*f)(); 这里声明有两个括号,第二个括号是函数调用,第一个括号是聚组作用.(*f)是一个函数,所以f是指向返回整型的函数的指针.程序中的每个函数都位于,内存中某个位置,所以存 ...

  9. Java NIO 系列教程

    http://www.iteye.com/magazines/132-Java-NIO

  10. resize

    resize 属性规定是否可由用户调整元素尺寸. resize: none|both|horizontal|vertical; none:用户无法调整元素的尺寸.      比较常用 both:用户可 ...