B. Bakery
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.

To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.

Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.

Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai ≠ b for every 1 ≤ i ≤ k) and choose a storage in some city s (s = aj for some 1 ≤ j ≤ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).

Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.

Input

The first line of the input contains three integers nm and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤ n) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.

Then m lines follow. Each of them contains three integers uv and l (1 ≤ u, v ≤ n, 1 ≤ l ≤ 109, u ≠ v) meaning that there is a road between cities u and v of length of l kilometers .

If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n) — the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.

Output

Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.

If the bakery can not be opened (while satisfying conditions) in any of the n cities, print  - 1 in the only line.

Examples
input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
output
3
input
3 1 1
1 2 3
3
output
-1
Note

Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.

题意:看懂题意就行了,有一些面粉厂,从其他的点到其中一个面粉厂最短路。

分析:

暴力扫边的题,活生生的被我建图成了迪杰斯特拉。其实也是仿照了网络流的姿势。

CF721C

C. Journey
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.

Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.

Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it.

Input

The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000,  1 ≤ m ≤ 5000,  1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.

The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.

It is guaranteed, that there is at most one road between each pair of showplaces.

Output

Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.

Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

If there are multiple answers, print any of them.

Examples
input
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4
input
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
output
4
1 2 4 6
input
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
3
1 3 5

题意:有向无环图,从1出发,到达节点n,在时间不超过T的情况下,最多经过多少个点。

分析:

咋一样,好像最短路分析不出来,有向无环图,说明从结点1出发,必定不能往回走了,这个时候应该考虑DP的,到达 i 节点不超过时间T(某一个时间)最多走多远,一个记忆化应该没有问题。

关于递推的写法,就很神奇了,d[ i ] [ t ] ,但是这两种方法,都需要反着建边。

有一种方式: d[ i ] [ j ] 走 i 个节点,到达 j 的最少时间。但是你会怎么到达 j 呢? 枚举边。很神奇~~~

#include <bits/stdc++.h>

using namespace std;

const int maxn = ;
const int maxm = ;
const int inf = 0x3f3f3f3f; int n,m,T;
struct Edge {
int u,v,w;
}; int d[maxn][maxn];
int pre[maxn][maxn]; vector<Edge> edges; int main()
{
//freopen("in.txt","r",stdin);
cin>>n>>m>>T; for(int i = ; i < m; ++i) {
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
edges.push_back((Edge){u,v,w});
}
memset(d,inf,sizeof(d)); d[][] = ;
int cnt = ;
for(int i = ; i <= n; i++) {
for(int j = ; j < m; j++) {
int u = edges[j].u;
int v = edges[j].v;
int w = edges[j].w; if(d[i-][u]+w < d[i][v]) {
d[i][v] = d[i-][u] + w;
pre[i][v] = u;
} }
if(d[i][n]<=T) {
cnt = i;
}
} cout<<cnt<<endl;
vector<int> ans;
ans.push_back(n);
while(true) {
ans.push_back(pre[cnt][n]);
n = pre[cnt][n];
cnt--;
if(n==) break;
} for(int i = ans.size()-;i >=; i--)
printf("%d ",ans[i]);
puts(""); return ;
}

ACM-ICPC (10/20)的更多相关文章

  1. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  2. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  3. Java in ACM/ICPC

    目录 Java在ACM/ICPC中的特点 在ACM/ICPC中使用Java需要注意的问题 Java与高精度计算 1.Java在ACM/ICPC中的特点 Java的语法和C++几乎相同 Java在执行计 ...

  4. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  5. ACM ICPC 2017 Warmup Contest 9 L

    L. Sticky Situation While on summer camp, you are playing a game of hide-and-seek in the forest. You ...

  6. ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))

    祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...

  7. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  8. hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  9. hduoj 4706 Children&#39;s Day 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...

  10. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. PIE SDK傅里叶变换

    1.算法功能简介 傅里叶变换能把遥感图像从空域变换到只包含不同频域信息的频域中.原图像上的灰度突变部位(如物体边缘).图像结构复杂的区域.图像细节及干扰噪声等,经傅里叶变换后,其信息大多集中在高频区: ...

  2. ansible 实战项目之文件操作(二)

    一,前言 如果没有安装好的话看我以前的贴子哦!! 上次安装已经确定通了,所以下面步骤应该是完全ok的 特点: (1).轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可: (2 ...

  3. apply、call、bind区别、用法

    apply和call都是为了改变某个函数运行时的上下文而存在的(就是为了改变函数内部this的指向):   如果使用apply或call方法,那么this指向他们的第一个参数,apply的第二个参数是 ...

  4. 九度oj题目1181:遍历链表

    题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2600 解决:1125 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1 ...

  5. nyoj 1239——引水工程——————【最小生成树 prim】

    引水工程 时间限制:2000 ms  |  内存限制:65535 KB 难度:3   描述 南水北调工程是优化水资源配置.促进区域协调发展的基础性工程,是新中国成立以来投资额最大.涉及面最广的战略性工 ...

  6. HDU 1166——敌兵布阵——————【线段树单点增减、区间求和】

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  7. 表单提交前的confirm验证提示

    今天要做一个修改提交前的提示,点击修改按钮进行提示,然后根据confirm的结果来决定是否提交;发现平时很常见的一个功能,自己不会.所以只能去晚上找资料了; 举例如下: <form action ...

  8. Class.forName和ClassLoader的区别

    一 看名字就知道了,一个是类的创建,一个类加载器 二 再看下Class.forName源码,调用了ClassLoader @CallerSensitive public static Class< ...

  9. [转]Oracle ROWNUM用法和分页查询总结

    本文转自:http://blog.csdn.net/fw0124/article/details/42737671 ****************************************** ...

  10. JavaScript 闭包初认识

    1.简单的例子 首先从一个经典错误谈起,页面上有若干个div, 我们想给它们绑定一个onclick方法,于是有了下面的代码 <ul id="divTest"> < ...