poj3268 Silver Cow Party(最短路)
非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了。
题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = 1e3 + ;
const int maxm = 1e5 + ;
const int inf = 0x3f3f3f3f;
struct edge{
int s, to, w, next;
} ed[maxm];
int n, m, x;
int mp[maxn][maxn], dis1[maxn], dis2[maxn];
bool vis[maxn];
inline int max( int a, int b ){
return a>b ? a:b;
} inline int min( int a, int b ){
return a<b ? a:b;
} inline int dij(){
//先求了去到x的最短路
memset( vis, , sizeof(vis) );
memset( dis1, inf, sizeof(dis1) );
dis1[x] = ;
for( int i=; i<n; i++ ){
int minid, MIN=inf;
for( int j=; j<=n ;j++ ) if( !vis[j] && MIN>dis1[j] ) MIN = dis1[minid=j];
vis[minid] = ;
for( int j=; j<=n; j++ ) if( !vis[j] ) dis1[j] = min(dis1[j], dis1[minid]+mp[minid][j]);
}
//接下来求回去的最短路
memset( vis, , sizeof(vis) );
memset( dis2, inf, sizeof(dis2) );
dis2[x] = ;
for( int i=; i<n; i++ ){
int minid, MIN = inf;
for( int j=; j<=n; j++ ) if( !vis[j] && MIN>dis2[j] ) MIN = dis2[minid=j];
vis[minid] = ;
for( int j=; j<=n; j++ ) if( !vis[j] ) dis2[j] = min( dis2[j], dis2[minid]+mp[j][minid] );
}
//遍历求出res
int res = -inf;
for( int i=; i<=n; i++ ) res = max( dis1[i]+dis2[i], res );
return res;
} int main(){
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(); //加速cin 和 cout读取速度,但是这样的话就不能使用scanf了
cin.tie();
cout.tie();
cin >> n >> m >> x;
memset( mp, inf, sizeof(mp) );
for( int i=; i<=n; i++ ) mp[i][i] = ;
for( int i=; i<m; i++ ){
int u, v, w;
cin >> u >> v >> w;
mp[u][v] = w;
}
cout << dij() << endl; return ;
}
poj3268 Silver Cow Party(最短路)的更多相关文章
- 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 ...
- 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算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- 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 ...
- poj3268 Silver Cow Party (SPFA求最短路)
其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...
- (poj)3268 Silver Cow Party 最短路
Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...
随机推荐
- 【Spring Boot学习之七】自定义参数&多环境配置&修改端口号&yml
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.自定义参数通过注解直接获取配置文件application.properties中配置key的value1.appl ...
- Java序列化的方式。
0.前言 本文主要对几种常见Java序列化方式进行实现.包括Java原生以流的方法进行的序列化.Json序列化.FastJson序列化.Protobuff序列化. 1.Java原生序列化 Java原生 ...
- .NET平台历程介绍
.Net平台的背景 1. 2010之前 的PC时代的时候,互联网规模还不是特别庞大,以静态编译式语言为代表的JAVA和.Net没什么太大区别,.net以windows自居. 2. 2010年以JAVA ...
- HLP帮助文件源文件RTF文件的编写
https://www.cnblogs.com/gaodu2003/archive/2008/12/17/1356861.html 举例说明如下: 每一节的标题在RTF文件中一般以特有的脚注($)指定 ...
- C指针的一些小细节
1 int *c;*c=4-->int *c;int b;c=&b;*c=4; 在使用指针之前,一定要将其初始化,当然,如果是赋予一个地址,就相当于使用的同时就进行了初始化.
- 【leetcode】590. N-ary Tree Postorder Traversal
Recurisve: /* // Definition for a Node. class Node { public: int val; vector<Node*> children; ...
- 上传docker镜像到阿里云镜像源
阿里云docker镜像配置 阿里云用户名可以使用淘宝系的,或者新注册都行. a. 配置阿里云的镜像加速器:加速器 然后在线上创建`镜像仓库`,需要设置`命名空间`和`仓库名称`,然后接着操作下面的步骤 ...
- 解决打开IE报错“无法启动...丢失api-ms-win-core-path-l1-1-0.dll”的问题
打开IE突然发现报错 试了各种方法都不行 最终看这篇文章,才解决:https://www.yijile.com/log/577.html 打开IE设置选项,选择管理加载项,如图讲该选项禁用,就不报错. ...
- el-select和el-cascader的visible-change下拉框隐藏时触发相关事件(下拉框下拉显示时不触发)
原文:https://blog.csdn.net/CarryBest/article/details/79959389 今天做项目时,用elementUI框架,需要下拉框隐藏时出发某个函数,用了vis ...
- SpringBoot中Logback日志的配置
说明 在SpringBoot中自带的日志工具是Logback,我们可以在Springboot的配置文件中直接对Logback进行一些简单的配置,如: logging.level.com.nowcode ...