PAT A 1018. Public Bike Management (30)【最短路径】
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)【最短路径】的更多相关文章
- 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 ...
- 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 ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- 1018 Public Bike Management (30)(30 分)
时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...
- 1018 Public Bike Management (30 分)
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- 1018 Public Bike Management (30分) 思路分析 + 满分代码
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
- 1018. Public Bike Management (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- PAT (Advanced Level) 1018. Public Bike Management (30)
先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include&l ...
随机推荐
- Linux--装好之后要做的几件事(转)
1.删除libreoffice libreoffice虽然是开源的,但是Java写出来的office执行效率实在不敢恭维,装完系统后果断删掉 sudo apt-get remove libreoffi ...
- Apache 80无法启动
netstat -abno后查看,经过查找为pid=4的system进程,因为是系统进程,也无法结束它,经查SQL Server ReportingServices (SQLEXPRESS) 服务占用 ...
- HTML5利用link标签的rel=import引入html页面
如果是以前,我们可以使用iframe去引入,现在可以是这样的形式:<link rel="import" href="a.html" id="tm ...
- insert into output使用
declare @t table (logId int,customerId int,amount int) insert into log( customerId,amount) output in ...
- hibernate UUID问题
前言:hibernate对于字符串类型主键支持UUID主键生成策略,(号称是世界上唯一的字符串) 运行环境:运行环境:hibernate5.2,mysql5.6 一,使用hibernate给Strin ...
- Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页、搜索、格式化、自定义按钮
系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...
- 第二轮冲刺-Runner站立会议04
今天:查看gridview与baseadapter的连接 明天准备做什么:继续gridview与baseadapter适配器 遇到的困难:暂无
- 面试题目——《CC150》位操作
面试题5.1:给定两个32位的整数N与M,以及表示比特位置的i与j.编写一个方法,将M插入N,使得M从N的第j位开始,到第i位结束.假定从j位到i位足以容纳M,也即若M=10011,那么j与i之间至少 ...
- 错误 Metadata file 'C:\Common\bin\Debug\Common.dll' could not be found
一个通用方法de类库/总是报这个错误/很明显就是没有成功生成程序集... 但是就是找不到哪里的错误!!!! 如果是代码写错的话,可能会直接提示在哪个文件中哪行代码写错了,然后dll生成不了,但是这个错 ...
- js闭包的作用域以及闭包案列的介绍:
转载▼ 标签: it js闭包的作用域以及闭包案列的介绍: 首先我们根据前面的介绍来分析js闭包有什么作用,他会给我们编程带来什么好处? 闭包是为了更方便我们在处理js函数的时候会遇到以下的几 ...