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 ...
随机推荐
- 上传到 App Store 时出错。
Try this, it fixed it for me. Open Terminal and run: cd ~ mv .itmstransporter/ .old_itmstransporte ...
- python web开发-flask中sqlalchemy的使用
SqlAlchemy是一个python的ORM框架. 在flask中有一个flask-sqlalchemy的扩展,使用起来很方便. 1. 创建一个sqlalchemy的Model模块 创建 ...
- Java 并发编程实践基础 读书笔记: 第三章 使用 JDK 并发包构建程序
一,JDK并发包实际上就是指java.util.concurrent包里面的那些类和接口等 主要分为以下几类: 1,原子量:2,并发集合:3,同步器:4,可重入锁:5,线程池 二,原子量 原子变量主要 ...
- 记录一个古老的Sql分页过程
/* 根据单位ID获取排班信息 For:WXX TIme:2017-11-22 */ ALTER proc [dbo].[proc_ScheduleInfo] )='', --单位ID )='', - ...
- Vue 知识复习(上)
Vue Vue实例 创建实例: var vm = new Vue({ //code }) 数据与方法: 只有当实例被创建时 data 中存在的属性才是响应式的; Vm.b = 'h1' 是不会触发视图 ...
- css3控制div上下跳动-效果图
效果图演示,源代码
- 如何在mac上搭建sqli-labs
近期想学习sql注入,但是一来网络上的资料参差不齐,难以系统的学习:二来随着程序员安全意识的提高,这种完全可以避免的注入漏洞越来越少见了,所以难以找一个合适的网站练手,于是乎,sqli-labs这种实 ...
- Rails 定时任务——whenever实现周期性任务
根据项目的进展,我们需要实现后台进行定时读取信息的功能,而最关键的实现部分是周期性功能,根据调研,决定使用whenever来实现这一功能. github:https://github.com/java ...
- 实验MyOD
实验MyOD 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 提交测试代码和运行结果截图,加上学号水印,提交码云代码链接. 代码如下: (刚开始 ...
- python array 使用创建10万浮点数
from array import array from random floats = array('d',random((for i in range(10**7)) fp = open('flo ...