JZOJ 3470. 【NOIP2013模拟联考8】最短路(path)
470. 【NOIP2013模拟联考8】最短路(path) (Standard IO)
Description
Input
接下来m行每行3个整数x、y、z,表示有一条从x到y的长为z的有向边。
接下来k行每行一个整数表示标记点编号。
Output
Sample Input
3 3 2 1 1
1 2 1
2 3 1
3 1 1
2
3
Sample Output
3
【样例解释】
路径为1->2->3->1。
Data Constraint
50%的数据n<=1000。
另有20%的数据k=0。
100%的数据n<=50000,m<=100000,0<=k<=10,1<=z<=5000。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#define N 100007
#define largest 100000000007
using namespace std;
int n, m, k, s, t, spe[], tot, ls[N], cnt;
long long ans, dis[N], d[][];
bool b[N], v[];
queue<int> q; struct edge
{
int to, next, w;
}e[N]; void add(int x, int y, int z)
{
e[++tot].to = y;
e[tot].w = z;
e[tot].next = ls[x];
ls[x] = tot;
} void spfa(int x)
{
memset(b, , sizeof(b));
for (int i = ; i <= n; i++)
dis[i] = largest;
dis[x] = ;
q.push(x);
while (!q.empty())
{
int now = q.front();
q.pop();
for (int i = ls[now]; i; i = e[i].next)
{
if (dis[now] + e[i].w < dis[e[i].to])
{
dis[e[i].to] = dis[now] + e[i].w;
if (!b[e[i].to])
{
q.push(e[i].to);
b[e[i].to] = ;
}
}
}
b[now] = ;
}
cnt++;
for (int i = ; i <= k + ; i++)
if (cnt != i) d[cnt][i] = dis[spe[i]];
d[cnt][k + ] = dis[t];
} void dfs(int dep, long long sum, int dc)
{
if (sum + d[dc][k + ] > ans) return;
if (dep >= k + )
{
if (sum + d[dc][k + ] < ans) ans = sum + d[dc][k + ];
return;
}
for (int i = ; i <= k + ; i++)
if (!v[i])
{
v[i] = ;
dfs(dep + , sum + d[dc][i], i);
v[i] = ;
}
} int main()
{
scanf("%d%d%d%d%d", &n, &m, &k, &s, &t);
for (int i = ; i <= m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
}
for (int i = ; i <= k + ; i++)
scanf("%d", &spe[i]);
spe[] = s;
for (int i = ; i <= k + ; i++)
spfa(spe[i]);
ans = largest;
dfs(, , s);
if (ans < largest) printf("%lld", ans);
else printf("-1");
}
JZOJ 3470. 【NOIP2013模拟联考8】最短路(path)的更多相关文章
- JZOJ【NOIP2013模拟联考14】隐藏指令
JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...
- JZOJ 3493. 【NOIP2013模拟联考13】三角形
3493. [NOIP2013模拟联考13]三角形(triangle) (File IO): input:triangle.in output:triangle.out Time Limits: 10 ...
- JZOJ 3487. 【NOIP2013模拟联考11】剑与魔法(dragons)
3487. [NOIP2013模拟联考11]剑与魔法(dragons) (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB De ...
- JZOJ 3463. 【NOIP2013模拟联考5】军训
3463. [NOIP2013模拟联考5]军训(training) (Standard IO) Time Limits: 2000 ms Memory Limits: 262144 KB Deta ...
- JZOJ 3462. 【NOIP2013模拟联考5】休息(rest)
3462. [NOIP2013模拟联考5]休息(rest) (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- JZOJ 3461. 【NOIP2013模拟联考5】小麦亩产一千八(kela)
3461. [NOIP2013模拟联考5]小麦亩产一千八(kela) (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Det ...
- 【NOIP2013模拟联考7】OSU
[NOIP2013模拟联考7]OSU 描述 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败之分, ...
- [jzoj]3468.【NOIP2013模拟联考7】OSU!(osu)
Link https://jzoj.net/senior/#main/show/3468 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: ...
- [jzoj]3456.【NOIP2013模拟联考3】恭介的法则(rule)
Link https://jzoj.net/senior/#main/show/3456 Description 终于,在众亲们的奋斗下,最终boss 恭介被关进了库特设计的密室.正当她们松了一口气时 ...
随机推荐
- Linux Shell命令系列(3)
11. chown命令 “chown”命令就是改变文件拥有者和所在用户组.每个文件都属于一个用户组和一个用户.在你的目录下,使用"ls -l",你就会看到像这样的东西.root@t ...
- python学习三(数据保存到文件)
以写模式打开文件:需要指定写模式,如下所示 data = open('data.out','w') 如果文件已经存在,则会清空它现有的所有内容.要追加一个文件,需要使用访问模式a,会追加到下一行. 例 ...
- Spring Cloud Ribbon负载均衡
目录 一.简介 二.客户端负载均衡 三.RestTemplate详解 GET请求 POST请求 PUT请求 DELETE请求 一.简介 Spring Cloud Ribbon是一个基于HTTP 和 ...
- Java 多线程的实现方法
package com.jckb; /**多线程实现的两种方法 * * @author gx * */ public class Test2 { public static void main(Str ...
- javascript获取滚动条位置(兼容所有浏览器)
有两种方式来获取浏览器滚动条的位置 第一种:document.documentElement.scrollTop 第二种:$("body").scrollTop() 第一种方式能够 ...
- SQL 分页实现
--通用分页 ALTER PROCEDURE [dbo].[Sys_Pagination_1] @tblName VARCHAR(2000) , -- 表名 @strGetFields VARCHAR ...
- 11个炫酷的 Linux 终端命令
我已经用了十年的Linux了,通过今天这篇文章我将向大家展示一系列的命令.工具和技巧,我希望一开始就有人告诉我这些,而不是曾在我成长道路上绊住我. 1.命令行日常系快捷键 如下的快捷方式非常有用,能够 ...
- vscode 常用插件安装
设置中文语言使用快捷键[Ctrl+Shift+P],弹出的搜索框中输入[configure language],然后选择搜索出来的[Configure Display Language],locale ...
- ABAP:从例子学习ABAP
1.插入内表行: *插入内表行: DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS ...
- 【ros depthimage_to_laser kinect2】
kinect2的深度图可以转换成激光来用,使用depthimage_to_laser 这个tf是用来给rviz显示的 1)开启kinect2 rosrun kinect2_bridge kinect2 ...