kuangbin专题最短路 D - Silver Cow Party
#include<iostream>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdio>
using namespace std;
#define MAXN 1004
#define INF 0x3f3f3f3f
/*
18 08
18 23
超时
18 24
18 33
超时
18 41 AC
有向图,所有结点中到终点来回距离的最大值
对每个节点求最短路得到to[MAXN],对终点求最短路的back[MAXN]
n3!超时!
只需将有向图反转一次(有向路径反向),对终点求两次Dijk即可
max(to+back)
*/
bool been[MAXN];
int lowcost[MAXN],g[MAXN][MAXN],m,n,x,ans=-;
int to[MAXN],back[MAXN];
void Read()
{
int x,y,d;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&x,&y,&d);
g[x][y] = d;
}
}
void Init()
{
memset(lowcost,INF,sizeof(lowcost));
memset(been,false,sizeof(been));
}
void Dijkstra(int beg,int tmp[])
{
Init();
lowcost[beg] = ;
for(int j=;j<n;j++)
{
int Min = INF,k = -;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]<Min)
{
Min = lowcost[i];
k = i;
}
}
if(k==-) break;
been[k] = true;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]>lowcost[k]+g[k][i])
{
lowcost[i] = lowcost[k]+g[k][i];
}
}
}
for(int i=;i<=n;i++)
{
if(i==beg) continue;
tmp[i] = lowcost[i];
}
}
int main()
{
scanf("%d%d%d",&n,&m,&x);
memset(g,INF,sizeof(g));
Read();
Dijkstra(x,back);
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
int t = g[i][j];
g[i][j] = g[j][i];
g[j][i] = t;
}
}
Dijkstra(x,to);
for(int i=;i<=n;i++)
{
ans = max(ans,back[i]+to[i]);
}
printf("%d\n",ans);
return ;
}
kuangbin专题最短路 D - Silver Cow Party的更多相关文章
- (最短路)Silver Cow Party --POJ--3268
题目链接: http://poj.org/problem?id=3268 题意: 先求出所有牛到x的最短路,再求出x到所有牛的最短路,两者相加取最大值(单向图)(可以用迪杰斯特拉,SPFA) 迪杰斯特 ...
- Silver Cow Party(最短路,好题)
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- 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 ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ3268 Silver Cow Party —— 最短路
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
- poj3268 Silver Cow Party(最短路)
非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. ...
随机推荐
- MySql学习笔记(1)-安装
一.安装环境 操作系统:win7 64ibt MySql版本:5.7.10.0 二.安装过程 1.点击Add 2.选择需要安装的组件 3.Excute 4.Next 5.Next 6.进入服务器配置 ...
- 揭开WebService的神秘面纱
一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...
- 安装linux mint 18.3 后要做的
使用u盘安装的linux mint 18.3,安装过程基本顺利 发现在安装过程中使用中文语言的话会使得下载附加软件的速度快很多 安装完成之后要做的事情有: 1.字体 默认的楷体字比较难看,在软件管理器 ...
- WebApi实现IHttpControllerSelector问题
一.让Web API路由配置也支持命名空间参数/// <summary> /// controller /// 选择器 /// </summary> ...
- from scipy import spatial 出现 from .qhull import * ImportError: DLL load failed: The specified module could not be found. 错误
错误描述: 本人机器window8.1 64位,python2.7. Traceback (most recent call last): File "C:/Users/Hamid/Docu ...
- Linux学习笔记之Linux命令
1. blkid 查看当前系统中所有已挂载文件系统的类型
- jstree中json data 的生成
jstree中json data 的生成 jstree官网上给出的json数据格式是这样的: <span style="font-size:14px;">// A ...
- NGS数据格式介绍
一般情况下,从Illumina平台上得到的测序,其数据格式是Fastq格式,可以称之为原始数据(Raw data).事实上直接的下机数据是显微拍摄得到的图像信息.但是一般都会用Bcl2Fastq软件将 ...
- Leetcode724:寻找数组的中心索引(java、python3)
寻找数组的中心索引 给定一个整数类型的数组 nums,请编写一个能够返回数组"中心索引"的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相 ...
- 洛谷——P2171 Hz吐泡泡
P2171 Hz吐泡泡 题目描述 这天,Hz大大心血来潮,吐了n个不同的泡泡玩(保证没有重复的泡泡).因为他还要写作业,所以他请你帮他把这些泡泡排序成树(左子树<=根<右子树).输出它的后 ...