poj3268 Silver Cow Party (SPFA求最短路)
其实还是从一个x点出发到所有点的最短路问题。来和回只需分别处理一下逆图和原图,两次SPFA就行了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
int n,m,x,a,b,t;
vector<int>vv1[maxn];
vector<int>vv2[maxn];
int t1[maxn][maxn],t2[maxn][maxn];
int d1[maxn],d2[maxn],ans=;
//以上1是原图的量,2是逆图的量
queue<int>q;
void spfa(int type)
{
if(type==)
{
q.push(x);
while(!q.empty())
{
int tt=q.front();
q.pop();
int st=vv1[tt].size();
for(int i=;i<st;i++)
{
int ti=vv1[tt][i];
if(d1[tt]+t1[tt][ti]<d1[ti])
{
d1[ti]=d1[tt]+t1[tt][ti];
q.push(ti);
}
}
}
}
else
{
q.push(x);
while(!q.empty())
{
int tt=q.front();
q.pop();
int st=vv2[tt].size();
for(int i=;i<st;i++)
{
int ti=vv2[tt][i];
if(d2[tt]+t2[tt][ti]<d2[ti])
{
d2[ti]=d2[tt]+t2[tt][ti];
q.push(ti);
}
}
}
}
}
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d%d%d",&n,&m,&x);
fill(d1,d1+maxn,INF);
fill(d2,d2+maxn,INF);
d1[x]=d2[x]=;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&t);
t1[a][b]=t2[b][a]=t;
vv1[a].push_back(b);
vv2[b].push_back(a);
}
spfa();
spfa();
for(int i=;i<=n;i++)
{
ans=max(ans,d1[i]+d2[i]);
}
printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
return ;
}
poj3268 Silver Cow Party (SPFA求最短路)的更多相关文章
- 基于bellman-ford算法使用队列优化的spfa求最短路O(m),最坏O(n*m)
acwing851-spfa求最短路 #include<iostream> #include<cstring> #include<algorithm> #inclu ...
- ACM - 最短路 - AcWing 851 spfa求最短路
AcWing 851 spfa求最短路 题解 以此题为例介绍一下图论中的最短路算法 \(Bellman\)-\(Ford\) 算法.算法的步骤和正确性证明参考文章最短路径(Bellman-Ford算法 ...
- 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 ...
- POJ3268 Silver Cow Party —— 最短路
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ3268 Silver Cow Party(dijkstra+矩阵转置)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15156 Accepted: 6843 ...
- spfa求次短路
思路:先算出每个点到1的最短路d1[i],记录下路径,然后枚举最短路上的边 删掉之后再求一遍最短路,那么这时的最短路就可能是答案. 但是这个做法是错误的,可以被卡掉. 比如根据下面的例题生成的一个数据 ...
- poj3268 Silver Cow Party(两次SPFA || 两次Dijkstra)
题目链接 http://poj.org/problem?id=3268 题意 有向图中有n个结点,编号1~n,输入终点编号x,求其他结点到x结点来回最短路长度的最大值. 思路 最短路问题,有1000个 ...
- POJ3268 Silver Cow Party Dijkstra最短路
Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...
- POJ3268 Silver Cow Party【最短路】
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...
随机推荐
- LeetCode-11-7
1.Reverse String Write a function that takes a string as input and returns the string reversed. Exam ...
- python常用模块——time模块
参考博客:http://blog.csdn.net/SeeTheWorld518/article/details/48314501 http://www.jb51.net/article/49325. ...
- pycharm一直scanning files to index
删除了c盘的垃圾文件之后,pycharm就一直scanning files to index 解决方法: 点击file,然后选择invalidate caches / restart ...,再弹出的 ...
- Android:日常学习笔记(7)———探究UI开发(4)
Android:日常学习笔记(7)———探究UI开发(4) UI概述 View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...
- PsySH——PHP交互式控制台
PsySH PsySH is a runtime developer console, interactive debugger and REPL for PHP. PsySH是一个PHP的运行时开发 ...
- Qt5.4.1移植到arm——Linuxfb篇
Qt5与Qt4对比有很大的改变,其最大的特性在于模块化,并且很明显的是不再见到Qt4用到的qws,Qt5新增了QPA系统,基于QPA使得Qt5移 植到一个新平台非常简单而又具有极强的底层扩展能力:同时 ...
- Windows系统 PHPstudy Apache无法启动的解决办法
最近在配置phpstudy的时候,出现是phpstudy apache无法启动的情况,其实也不是一点也不能启动,而且apache的启动状态亮一下就自动关闭了. 这样情况大部分小伙伴应该都遇到过,以前看 ...
- Variation calling and annotation
Resequencing 302 wild and cultivated accessions identifies genes related to domestication and improv ...
- spring boot 打包
打成jar直接运行比较简单,这里特别说明的是打成war包,部署在外部的tomcat方式: 1.修改启动代码 import org.springframework.boot.SpringApplicat ...
- 字符串匹配算法BF和KMP总结
背景 来看一道leetcode题目: Implement strStr(). Returns the index of the first occurrence of needle in haysta ...