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. noip2016十连测round2

    A: Divisors 题意:给定 m 个不同的正整数 a 1 ,a 2 ,...,a m ,请对 0 到 m 每一个 k 计算,在区间 [1,n] 里有多少正整数 是 a 中恰好 k 个数的约数. ...

  2. Nginx反向代理tomcat,seesion会话保持。

    proxy_pass http://10.0.0.10:8081/monitor/; proxy_cookie_path /monitor/ /; proxy_set_header Host &quo ...

  3. BZOJ1483: [HNOI2009]梦幻布丁

    传送门 名字起得很高端实际上很简单的算法hhh 启发式合并 简单讲就是一些合并一堆队列的题可以用启发式合并,或者说这是一个思想.每次把小的合并到大的部分,均摊复杂度$O(MlogN)$. //BZOJ ...

  4. <<< Java生成Md5

    package cn.sevennight.test; import java.security.MessageDigest; public class md5{ public static Stri ...

  5. C# 4.0 之线程安全集合篇

    资料:http://www.cnblogs.com/chengxiaohui/articles/5672768.html

  6. 项目里面的某个.m文件无法使用

    - 检查:Build Phases -> Compile Sources

  7. 优化MySQL数据库性能的八大方法

    本文探讨了提高MySQL 数据库性能的思路,并从8个方面给出了具体的解决方法. 1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就 ...

  8. jquery 页面加载时获取图片高度

    $(function () { $(window).load(function(){ alert($('img').height()); }); });

  9. [BZOJ1112][POI2008]砖块Klo

    [BZOJ1112][POI2008]砖块Klo 试题描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另 ...

  10. Apache curator-client详解

    Apache curator框架中curator-client组件可以作为zookeeper client来使用,它提供了zk实例创建/重连机制等,简单便捷.不过直接使用curator-client并 ...