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 ...
随机推荐
- celery的安装和使用
celery是python开发的分布式任务调度模块,接口简单,开发容易,五分钟就写出一个异步发送邮件的服务,celery本身不含消息服务,它使用第三方消息服务来传递任务,目前,celery支持的消息服 ...
- hadoop linux 杂记
切换到root su 修改sudo sudo + 命令 --> root权限 + 命令 su root vim /etc/sudoers ...
- docker-compose no such image
是由于docker-compose旧缓存的问题,执行docker-compose down即可,再重新up
- Hibernate一对多、多对一关联
一对多.多对一关联:在多方加外键 示例:Group(一方)和User(多方),一个Group可以有多个User,每个User只能属于一个Group 多对一单向关联 在User(多方)中建Group ...
- java WordUtils word文档工具类
package com.sicdt.library.core.utils; import java.io.ByteArrayOutputStream; import java.io.IOExcepti ...
- 关于Class.getResourceAsStream
Properties properties = new Properties(); properties.load(new InputStreamReader(CharactorTest.cl ...
- 无线路由:关于WDS,Repeater等模式的说明
转:http://blog.csdn.net/lizhiqiang5846/article/details/38397803 当今如果不用无线路由连接宽带似乎是很不popular了,当然Wifi/WL ...
- 8.14比赛j题 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#overview
就我个人来说我觉得这道题其实不用写题解,只是因为做的时候错了一次,如果不是队友细心,我根本会错下去,所以我感觉自己必须强大#include<stdio.h> #include<str ...
- centos6 多段Ip添加脚本
#!/bin/bash export device=`ifconfig|grep eth0|head -n 1|awk '{print ($1)}'`export ipcfg_pre="/e ...
- java格式化输出 printf 例子
import java.util.Date; public class Printf { public static void main(String[] args) { // %s表示输出字符串,也 ...