470. 【NOIP2013模拟联考8】最短路(path) (Standard IO)

Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits  

Description

给定一个n个点m条边的有向图,有k个标记点,要求从规定的起点按任意顺序经过所有标记点到达规定的终点,问最短的距离是多少。
 

Input

第一行5个整数n、m、k、s、t,表示点个数、边条数、标记点个数、起点编号、终点编号。

接下来m行每行3个整数x、y、z,表示有一条从x到y的长为z的有向边。

接下来k行每行一个整数表示标记点编号。

Output

输出一个整数,表示最短距离,若没有方案可行输出-1。
 

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

20%的数据n<=10。

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)的更多相关文章

  1. JZOJ【NOIP2013模拟联考14】隐藏指令

    JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...

  2. JZOJ 3493. 【NOIP2013模拟联考13】三角形

    3493. [NOIP2013模拟联考13]三角形(triangle) (File IO): input:triangle.in output:triangle.out Time Limits: 10 ...

  3. JZOJ 3487. 【NOIP2013模拟联考11】剑与魔法(dragons)

    3487. [NOIP2013模拟联考11]剑与魔法(dragons) (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  De ...

  4. JZOJ 3463. 【NOIP2013模拟联考5】军训

    3463. [NOIP2013模拟联考5]军训(training) (Standard IO) Time Limits: 2000 ms  Memory Limits: 262144 KB  Deta ...

  5. JZOJ 3462. 【NOIP2013模拟联考5】休息(rest)

    3462. [NOIP2013模拟联考5]休息(rest) (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed ...

  6. JZOJ 3461. 【NOIP2013模拟联考5】小麦亩产一千八(kela)

    3461. [NOIP2013模拟联考5]小麦亩产一千八(kela) (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Det ...

  7. 【NOIP2013模拟联考7】OSU

    [NOIP2013模拟联考7]OSU 描述 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败之分, ...

  8. [jzoj]3468.【NOIP2013模拟联考7】OSU!(osu)

    Link https://jzoj.net/senior/#main/show/3468 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: ...

  9. [jzoj]3456.【NOIP2013模拟联考3】恭介的法则(rule)

    Link https://jzoj.net/senior/#main/show/3456 Description 终于,在众亲们的奋斗下,最终boss 恭介被关进了库特设计的密室.正当她们松了一口气时 ...

随机推荐

  1. 使用:/usr/bin/phpize 报错

    使用:/usr/bin/phpize 出现下面错误提示 Can't find PHP headers in /usr/include/php The php-devel package is requ ...

  2. PHP实例:使用PHPExcel导入Excel2003文档和Excel2007文档到MySQL数据库中

    如果要使用phpExcelReader将Excel 数据导入到mysql 数据库,请读者点击这个文章查看. 使用phpExcelReader将Excel 数据导入到mysql 数据库. 下面我们介绍另 ...

  3. Windows安全认证是如何进行的?[Kerberos篇]

    最近一段时间都在折腾安全(Security)方面的东西,比如Windows认证.非对称加密.数字证书.数字签名.TLS/SSL.WS-Security等.如果时间允许,我很乐意写一系列的文章与广大网友 ...

  4. HDU 5445——Food Problem——————【多重背包】

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  5. orcale错题分析

    删除同义词语法正确的是: Drop  synonym sy nonym_name; 关于Oracle创建间隔分区后,正确的是: 使用partition(分区名)可以查看特定分区内存放的表记录 关于序列 ...

  6. springboot 学习笔记(四)

    (四)springboot整合mybatis 1.以mysql为例,在pom文件中添加如下依赖,依次为mybatis.jdbc.db pool依赖 <dependency> <gro ...

  7. ps使用

    1.图片剪裁 1.按快捷键M(矩形选择工具)-> 选中要扣出的图片(按shift可正方形)->按快捷键C(剪裁工具)->双击鼠标选中区域,剪裁成功. 2.选中psd中的图标 1.按快 ...

  8. Vue-mixins选项

    Vue-mixins选项 Mixins用于: 1.已经写好了构造器,还要增加方法或者临时的活动时使用的方法,用混入能减少源代码的污染. 2.公用方法,用混入的方法可以减少代码量,实现代码重用.(使用全 ...

  9. PHP报错configure error Cannot find libmysqlclient under usr

    编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法 (问题产生,mysql是yum安装的,libmysqlclient* ...

  10. python3基础05(有关日期的使用1)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import timefrom datetime import datetime,timedelta,timez ...