POJ-3268 Silver Cow Party---正向+反向Dijkstra
题目链接:
https://vjudge.net/problem/POJ-3268
题目大意:
有编号为1-N的牛,它们之间存在一些单向的路径。给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求所有牛从开始到回家的时间是多少?
思路:
所有牛都回到了家所花费的时间就是这些牛中花费时间的最大者,可以正向的Dijkstra求出从X到每个点的最短时间,然后一个骚操作:将所有边反向(swap(Map[i][j], Map[j][i])),再从x用dijkstra求出X到每个点的最短时间,第二次求出来的时间通过逆向求出来的是每个点到X的最短时间,两个一加,取最大者就是答案。
本题也可以用Bellman来求,不过由于边比较多,必须用队列优化的SPFA来求,但是SPA需要邻接表,不能像邻接矩阵那样直接反向,所以存图的时候存两张图,一张正向,一张反向,跑俩遍出结果。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<sstream>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 1e9 + ;
int T, n, m, cases;
int Map[maxn][maxn];
int d1[maxn], d2[maxn];
bool v[maxn];
void flip()
{
for(int i = ; i <= n; i++)
{
for(int j = i + ; j <= n; j++)swap(Map[i][j], Map[j][i]);
}
}
void dijkstra(int u, int d[])
{
memset(v, , sizeof(v));
for(int i = ; i <= n; i++)d[i] = INF;
d[u] = ;
for(int i = ; i <= n; i++)
{
int x, m = INF;
for(int i = ; i <= n; i++)if(!v[i] && d[i] <= m)m = d[x = i];//找距离源点最近的点
v[x] = ;
for(int i = ; i <= n; i++)d[i] = min(d[i], d[x] + Map[x][i]);//进行松弛
}
}
int main()
{
int u, x, y, z;
while(cin >> n >> m >> u)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)Map[i][j] = (i == j ? : INF);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &x, &y, &z);
//cout<<"000"<<endl;
Map[x][y] = z;
}
dijkstra(u, d1);
flip();
dijkstra(u, d2);
for(int i = ; i <= n; i++)d1[i] += d2[i];
sort(d1 + , d1 + n + );
cout<<d1[n]<<endl;
}
return ;
}
POJ-3268 Silver Cow Party---正向+反向Dijkstra的更多相关文章
- 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 (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj 3268 Silver Cow Party(最短路dijkstra)
描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...
- 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、反向建图】
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 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵
Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
随机推荐
- Java String常用方法
字符串查找 两种查找字符串的方法,indexOf(String s)和lastIndexOf(String s). String str = "tyson-json"; int i ...
- MIP (百度移动网页加速器)
前言:第一次用移动网页加速器,感觉好心情都被弄坏了.确实性能提高了不少,但是限制js,对于一些交互实现都成问题.MIP是Mobile Instant Pages的缩写,指百度移动网页加速器, 是一套应 ...
- c++ --> const关键字总结
const关键字总结 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性.const 是C++中常用的类型修饰符,常类型是指使用类型修饰符const说明的类型,常类型的变 ...
- 套接字API
Q:套接字特点 A:管道,消息队列,信号量,共享内存这些通信机制只能允许同一计算机上运行的进程相互通信,而套接字不仅可以提供在同一计算机上的进程间通信,还可以提供不同计算机上的进程间通信. 服务器端: ...
- kvm之四:从网上镜像安装虚拟机Centos6.8
1.再加块硬盘,格式化挂载至新建目录/kvm2下 2.CentOS 6.8镜像地址 http://mirrors.163.com/centos/6.8/os/x86_64/ 3.配置安装参数,执行安装 ...
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...
- Vim编辑器的注释,解注,删除与恢复
1. 注释: 将光标移动到注释首部 命令模式下 Ctrl+V,进入列模式 上下移动,选中待注释内容 按大写I,进入插入模式 输入 // or # 按两次退出 2 解注 将光标移动到待解注首部 命令模式 ...
- python中Properties的一些小用法
property最大的用处就是可以为一个属性制定getter,setter,delete和doc,他的函数原型为: def __init__(self, fget=None, fset=None, f ...
- JavaEE HttpServlet 解析
上一篇 文章中有提到,GenericServlet 是对 Servlet 的一个通用实现,并提供了一个抽象的 service() 方法.而我们的互联网是使用 Http 协议来通信的,那针对这个&quo ...
- Leetcode 35——Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...