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. php网址显示excel表格内容

    /** * excel表格内容在网页中显示 * * 首先需要下载PHPExcel 工具包 * 网址: http://phpexcel.codeplex.com/releases/view/119187 ...

  2. C# winform开发:Graphics、pictureBox同时画多个矩形

    C#的System.Drawing 命名空间提供了对 GDI+ 基本图形功能的访问 重点在于获取Graphics对象,例如: Graphics g = panel1.CreateGraphics 事实 ...

  3. gis数据格式转换(数据导入)ConvertFeaCls

    本文主要对数据导入等里 常用的不同格式数据创建.转换等 进行代码示例.主要用到IFeatureDataConverter.ConvertFeatureClass方法. 代码如下,难度不大,只是个技巧问 ...

  4. 10 件有关 JavaScript 让人费解的事情

    JavaScript 可算是世界上最流行的编程语言,它曾被 Web 开发设计师贴上噩梦的标签,虽然真正的噩梦其实是 DOM API,这个被大量的开发与设计师随手拈来增强他们的 Web 前端的脚本语言, ...

  5. mysql遇到锁表常用命令

    出现 waiting for table metadata lock 锁表的解决方法 1. show processlist; kill xxx; //xxx 为会话id 2.查询是否有未提交的事物 ...

  6. Idea 开发 web项目

    1.经历 很久没有搞 web 项目了,最近一段时间搞过很多次了,但是总是在 mac 上部署失败. 2.方法: 用idea 新建一个模板的 Spring MVC 项目,部署就可以了. 3.参考: htt ...

  7. java基本知识小记(1)

    1.Java中的值传递 值传递意味着对于传给方法的每个参数都会制作一份副本然后将副本而不是原始值传递给方法并且通过参数的名进行引用. 注意:虽然传值机制对于所有参数类型都适用,但是他对对象类型的作用与 ...

  8. 【荐】PHP采集工具curl快速入门教程

    为什么要用CURL? CURL(Client URL Library Functions)是一个利用URL语法在命令行方式下工作的文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTT ...

  9. BZOJ1264——[AHOI2006]基因匹配Match

    1.题意,求最长公共子序列,每个数字在序列中都出现5次 2.分析:最长公共子序列的标准解法是dp,$O(n^2)$过不了的,然后我们发现判断哪两个位置优化的地方用$5n$就可以搞定了,那么我们用BIT ...

  10. word20161211

    H.323 half-duplex / 半双工 handle count / 句柄数 handshaking / 握手 Hardware Compatibility List, HCL / 硬件兼容性 ...