图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12674   Accepted: 5651

Description

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

Source


 

Mean:

原意:草场上有n个农场,农场之间有一些路径,每个农场里住着一头牛,现在x农场的牛要过生日开party,其他农场的牛要到该农场去参加party,现在让你选择一头来回耗时最多的一头牛出来,输出时间。

给你一个n个结点、m条边的有向图,现在要你求从n-1个结点到达指定的一个结点的来回最长路。

 

analyse:

这题思路很巧妙,我们在存图的时候用链式向前星来存,存的时候就建两次边,一次正向,一次反向,用一个flag来标记一下。然后用两遍spfa,第一遍求出从x点出发的正向图到每个结点的最短路,第二遍求出从x点出发的反向图到每个结点的最短路,最后将两次的最短路对应相加,求出最大值即为最终的answer。

 

Time complexity:O(n*k)

 

Source code:

 

 

  

 

posted @   北岛知寒  阅读(272)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩