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 ...
随机推荐
- redis3.2.11多机多实例集群部署及测试连接情况
机器配置 redis3.2.11安装配置规划 机器 192.168.169.136(本机虚拟机1) 192.168.169.137(本机虚拟机2) 系统 Red Hat Enterprise Linu ...
- python基础23 -----进程和线程
一.进程 1.什么是进程? 1.1 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成. 1.2 程序是指进程需要完成那些功能以及如何完成. 1.3 数据集 ...
- (from) Javascript 生成指定范围数值随机数
from:http://blog.csdn.net/ilibaba/article/details/3741786 查手册后才知道, 介绍的信息少得可怜呐, 没有介绍生成 m-n 范围的随机数..., ...
- KVM虚拟化虚拟机支持虚拟化
一.开启的时候需要关闭所有虚拟机: 首先检查 KVM host(宿主机/母机)上的kvm_intel模块是否打开了嵌套虚拟机功能(默认是开启的): 1.modinfo kvm_intel | grep ...
- freeMarker入门示例
1.创建Web项目freeMarkerDemo. 2.添加jar包---freemarker-2.3.9.jar. 3.在WebContent目录下新建templates文件夹,用于放置模板文件ftl ...
- Linux图像系统框架-理解X11与Qt的层次结构
转:http://blog.csdn.net/kjfureone/article/details/52848550 1. 前言 图形子系统是linux系统中比较复杂的子系统之一:对下,它要管理形态各异 ...
- [POI2008]砖块Klo
题目 爆炸\(OJ\)机子太慢了吧实在不想打平衡树了 做法 烂大街的一个概念:求中位数 然后求前缀差和后缀差,主席树模板题 注意\(int\)和\(long long\) My complete co ...
- python:字典的方法
1.查找字典中的key对应的值和key是否存在(get,has_key)dict.get(key, default = None) :返回字典中key对应的值,若key不存在字典中,则返回defaul ...
- 20145230《java程序设计》 第四次实验报告
20145230实验4 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件 ...
- Docker 跨主机网络
Docker提供两种原生的跨主机网络: Overlay 和 Macvlan libnetwork & CNM libnetwork 是 docker 容器网络库,最核心的内容是其定义的 C ...