POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路
Silver Cow Party
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
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai,Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
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
#include<stdio.h>
#include<string.h>
#include<deque>
#include<vector>
#define MAX 1005
#define INF 0x3f3f3f3f
using namespace std; struct Node{
int v,w;
}node;
vector<Node> edge[MAX],redge[MAX];
int dis[MAX],diss[MAX],b[MAX];
int n;
void spfa(int k)
{
int i;
deque<int> q; //双向队列
for(i=;i<=n;i++){
dis[i]=INF;
diss[i]=INF;
}
memset(b,,sizeof(b));
b[k]=;
dis[k]=;
q.push_back(k);
while(q.size()){
int u=q.front();
for(i=;i<edge[u].size();i++){
int v=edge[u][i].v;
int w=edge[u][i].w;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
if(b[v]==){
b[v]=;
if(dis[v]>dis[u]) q.push_back(v); //SLF
else q.push_front(v);
}
}
}
b[u]=;
q.pop_front();
}
b[k]=;
diss[k]=;
q.push_back(k);
while(q.size()){
int u=q.front();
for(i=;i<redge[u].size();i++){
int v=redge[u][i].v;
int w=redge[u][i].w;
if(diss[v]>diss[u]+w){
diss[v]=diss[u]+w;
if(b[v]==){
b[v]=;
if(diss[v]>diss[u]) q.push_back(v); //SLF
else q.push_front(v);
}
}
}
b[u]=;
q.pop_front();
}
}
int main()
{
int m,x,u,v,w,i;
scanf("%d%d%d",&n,&m,&x);
for(i=;i<=n;i++){
edge[i].clear();
redge[i].clear();
}
for(i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
node.v=v;
node.w=w;
edge[u].push_back(node);
node.v=u;
redge[v].push_back(node);
}
spfa(x);
int max=;
for(i=;i<=n;i++){
if(dis[i]+diss[i]>max&&dis[i]!=INF&&diss[i]!=INF) max=dis[i]+diss[i];
}
printf("%d\n",max);
return ;
}
POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路的更多相关文章
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
随机推荐
- nexus搭建maven私服及私服jar包上传和下载
nexus搭建maven私服及私服jar包上传和下载 标签: nexus管理maven库snapshot 2017-06-28 13:02 844人阅读 评论(0) 收藏 举报 分类: Maven(1 ...
- 如何在ubuntun中安装pycharm并将图标显示在桌面上
安装pycharm首先要安装jdk. 可以通过java -V来查看是否安装了jdk.安装jdk的方法如下: 1 首先在oracle网站下载jdk,现在jdk是1.8的. 2 新建一个/usr/lib/ ...
- (转)js中__proto__和prototype的区别和关系
作者:doris链接:https://www.zhihu.com/question/34183746/answer/58155878来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- Java for LeetCode 125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 7-4 汉密尔顿回路(25 分) 【STL】
7-4 汉密尔顿回路(25 分) 著名的"汉密尔顿(Hamilton)回路问题"是要找一个能遍历图中所有顶点的简单回路(即每个顶点只访问 1 次).本题就要求你判断任一给定的回路是 ...
- spring-boot2代码
App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- TopCoder SRM420 Div1 RedIsGood —— 期望
题目链接:https://vjudge.net/problem/TopCoder-9915 (论文上的题) 题解: 更正:, i>0, j>0 代码如下: #include <ios ...
- blog真正的首页
声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 上一节我们阐明了django的开发流程, ...
- OpenCV——PS 滤镜算法之平面坐标到极坐标的变换
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...