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 ...
随机推荐
- Log level with log4j and Spark
Log Level Usages OFF This is the most specific, which allows no logging at all FATAL This is the mos ...
- uuid的使用
1.mysql中直接使用uuid()函数,可以生成一个随机的uuid 正常的uuid是36位长度的,其中有4个字符是‘-’,在mysql中可以使用replace()函数来替换‘-’ insert in ...
- Service Fusing
服务熔断也称服务隔离,来自于Michael Nygard 的<Release It>中的CircuitBreaker应用模式,Martin Fowler在博文CircuitBreaker中 ...
- curl操作封装
<?php /** * Class Curl curl简单封装 get post */ class Curl { /** * @brief get请求 * @param $url 请求的url ...
- Linux Shell编程 条件判断语法
if条件判断语句 单分支 if 条件语句 语法格式: if [条件判断式];then 程序 fi 或者 if [条件判断式] then 程序 fi 在使用单分支 if 条件查询时需要注意几点: if ...
- 通过调节坐标进行jfree图的放大缩小
http://blog.csdn.net/lt1983lt/article/details/5665085 import Java.awt.BorderLayout;import java.awt.C ...
- scope的继承
本文转载自: http://www.tuicool.com/articles/63iEref angular中scope的继承与js的原型继承有密切的联系,首先回顾一下js的继承: function ...
- myisam表修复
数据库myisam引擎表损坏修复步骤: 1.进入到表目录文件下 # myisamchk -of comments.MYI 2. # myisamchk -r comments.MYI 3. # ...
- Kubernetes Metrics-Server
github地址:https://github.com/kubernetes-incubator/metrics-server 官网介绍:https://kubernetes.io/docs/task ...
- Go reflect反射
Go语言中的反射非常强大,可以对string, int, struct, func...进行反射,使用起来也比较简单. 示例1:反射函数 package main import ( "fmt ...