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 ...
随机推荐
- mybatis学习日记-day01
Mybatis说明: MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的 ...
- Axis1.4之定制发布服务
将axis1.4_home\webapps目录下的axis文件夹拷贝到tomcat_home\webapps目录下.然后在tomcat_home\webapps\axis\WEB-INF\lib下添加 ...
- Docker 网络管理及容器跨主机通信
1.网络模式 docker支持四种网络模式,使用--net选项指定: host,--net=host,如果指定此模式,容器将不会获得一个独立的network namespace,而是和宿主机共用一个. ...
- 部署腾讯云(CentOS6.6版本,jdk1.7+tomcat8+mysql)
这是从一个大神哪里学到的,用来留下来用以记录 http://blog.csdn.net/qingluoII/article/details/76053736 只是其中有一个地方,我在学习的时候觉得可以 ...
- 后台运行之[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]
// 正常程序退出后,会在几秒内停止工作: // 要想申请更长的时间,需要用到 // beginBackgroundTaskWithExpirationHandler // endBackground ...
- 蓝牙4.0模块控制LED彩灯调光调色经验之谈
基于蓝牙模块的智能LED彩灯调光调色控制思路如下: 在此,找一个低功耗蓝牙模块内嵌接入LED灯的控制电路板,接入LED彩灯的控制电路中. 蓝牙模块彩灯控制方式如下,本文两类来解说led灯的控制方式: ...
- Oracle查询优化改写--------------------报表和数据仓库运算
一.行转列 二.列传行 '
- 【Android】带进度条的WebView
http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
- Restful风格,PUT修改功能请求,表单中存在文件报错-HTTP Status 405 - Request method 'POST' not supported
解决方案配置如下 <!-- 配置文件上传解析器 --> <bean id="multipartResolver" class="org.springfr ...
- mui手机图片压缩上传+C#
前台参考网址:http://www.bcty365.com/content-146-3263-1.html <html> <head> <meta charset=&qu ...