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. .NET 微信Token验证和消息接收和回复

    public class wxXmlModel { public string ToUserName { get; set; } public string FromUserName { get; s ...

  2. COGS396. [网络流24题]魔术球问题(简化版

    问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为 1,2,3,4......的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全平 ...

  3. BZOJ3747: [POI2015]Kinoman

    传送门 线段树经典运用. 设$last_i$表示上一个与$i$相同的类型.然后每次更新$[last[i]+1,i]$和$[last[last[i]]+1,last[i]]$的答案就行了. //BZOJ ...

  4. backup mysql

    #!/bin/bashcd /home/Licw/backup_openDBNow=$(date +"%m-%d-%Y--%H:%M:%S")#echo $NowFile=$Now ...

  5. Windows10环境搭建Elasticsearch+Kibana+Marvel

    环境: Windows10企业版X64 Elasticsearch-2.4.1 Kibana-4.6.1 Marvel-2.0+ 步骤: 安装Elasticsearch:官网下载Elasticsear ...

  6. Effective java读书笔记

    2015年进步很小,看的书也不是很多,感觉自己都要废了,2016是沉淀的一年,在这一年中要不断学习.看书,努力提升自己 计在16年要看12本书,主要涉及java基础.Spring研究.java并发.J ...

  7. 搭建haproxy

    1:下载haproxy wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz2:解压,编译,安装 tar zxf hapr ...

  8. 设置arc 的默认编辑器

    arc set-config editor "vim" 转自:http://udn.yyuap.com/thread-39791-1-1.html Pharicator是FB的代码 ...

  9. 大组合数:Lucas定理

    最近碰到一题,问你求mod (p1*p2*p3*……*pl) ,其中n和m数据范围是1~1e18 , l ≤10 , pi ≤ 1e5为不同的质数,并保证M=p1*p2*p3*……*pl ≤ 1e18 ...

  10. http_build_query 的一个问题

    当我们使用CURL来post数据的时候,需要设置post的数据 curl_setopt($c, CURLOPT_POSTFIELDS, $post_data); 假如这里的$data是 $data = ...