POJ 3268 Silver Cow Party 最短路径+矩阵转换
Silver Cow Party
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
<i>N</i>, <i>M</i>, and <i>X</i>
<br>Lines 2..<i>M</i>+1: Line <i>i</i>+1 describes
road <i>i</i> with three space-separated integers:
<i>A<sub>i</sub></i>,
<i>B<sub>i</sub></i>, and
<i>T<sub>i</sub></i>. The described road runs from farm
<i>A<sub>i</sub></i> to farm
<i>B<sub>i</sub></i>, requiring
<i>T<sub>i</sub></i> time units to traverse.
must walk.
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
题意:一群牛分别从1~n号农场赶往x号农场参加聚会,农场与农场之间的路时单向的,在n个农场之间有m条路,给出 a ,b , t表示从a号农场到b号农场需要t时间。 每头牛都会选择最短的路,问来回路上(i→x+x→i)花费时间最长的牛花费的时间是多少?
题解:一眼看过去很简单,先计算x农场到其他农场用的最短时间,在枚举其他农场到x农场的最短时间,记录下最大来回时间即可。的确,不过我们算一算时间复杂度,在枚举其他农场到x农场的最短时间时,最大复杂度为O(n^3)。也就是1000^3,很明显超过了2000ms。所以我们要想办法把枚举过程的复杂度降下来。 这里可以采用置换矩阵,因为是路径时单向的,我们交换 map[i][j] 与 map[j][i] 的值,那么枚举过程就变成了求x到其他农场的最短时间,这里就变成了O(n^2)的算法。
我用了两次dijkstra
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define inf 0x3f3f3f3f;
using namespace std;
int n, m, s;
int e[][];
int d1[];
int d2[];
int v[];
void dijstra(int s, int *d)
{
int i,j;
for (i = ; i <= n; i++)
{
d[i] = e[s][i];
v[i] = ;
}
v[s] = ;
for (i = ; i <= n - ; i++)
{
int k = -;
int mi = inf;
for (j = ; j <= n; j++)
{
if (v[j] == && d[j] < mi)
{
mi = d[j];
k = j;
}
}
if (k == -) break;
v[k] = ;
for (j = ; j <= n; j++)
{
if (v[j] == && d[j] > d[k] + e[k][j])
{
d[j] = d[k] + e[k][j];
}
}
}
}
int main()
{
int i, j;
cin >> n >> m >> s;
memset(e, 0x3f3f3f3f, sizeof(e));
for(i=;i<=;i++) e[i][i]=;
for (i = ; i <= m; i++)
{
int x, y, z;
cin >> x >> y >> z;
if (e[x][y] > z)
{
e[x][y] = z;
}
}
dijstra(s, d1);
for (i = ; i <= n; i++)
{
for (j = ; j < i; j++)
{
int temp = e[i][j];
e[i][j] = e[j][i];
e[j][i] = temp;
}
}
dijstra(s, d2);
int mm = ;
for (i = ; i <= n; i++)
{
if (d1[i] + d2[i] > mm)
{
mm = d1[i] + d2[i];
}
}
cout << mm << endl;
return ;
}
POJ 3268 Silver Cow Party 最短路径+矩阵转换的更多相关文章
- 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 最短路—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 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- 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:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- poj 3268 Silver Cow Party
S ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
随机推荐
- matplotlib小示例
matplotlib 画廊 http://matplotlib.org/gallery.html import numpy as np import matplotlib.pyplot as plt ...
- union-find算法
1.背景 <算法>一书中提到了关于算法的一些基本思想 优秀的算法因为能够解决实际的问题而变得更为重要: 高效算法的代码可以很简单: 理解某个实现的性能特点是一项有趣而令人满足的挑战: 在 ...
- WebGL编程指南高级技术篇(常见需求的处理)
一.鼠标控制模型旋转 实质的根据鼠标移动前后的位置比较得出x,y轴的旋转角度: 图中是一个屏幕,有一个模型(恩,他是一个模型),鼠标由P点移动到P1点,我们假定移动单位步长旋转β角度: P(x1,y1 ...
- SWIFT中切換UIContainerView內的Controller
如下,一个UIContainerView内切换两个Controller,当点击登录的时候UIContainerView的视图为LoginController,当点击登记的时候UIContainerVi ...
- Activity的四大启动模式
在自己清单中的Activity里配置这四大启动之一. stander 标准模式 先进后出 singletop 会检查栈顶如果有,那么就复用,不会重新开启. singletask ...
- python海龟绘图
最近学了python,看了几本书之后,才明白python的强大,python是一种解释型的语言,即每写一行程序就执行一行. 而且在科学计算方面,处理的能力特别的方便. 比如python中的字典dict ...
- Unity3D游戏-愤怒的小鸟游戏源码和教程(二)
Unity愤怒的小鸟游戏教程(二) 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) AngryEva游戏效果 ...
- 动态绑定AJAX,获取下级分类并延迟执行
HTML: <div id='allType'> <div class='allTypeHead'><span>所有分类</span></div& ...
- Tomcat:Several ports are already in use问题
Several ports (8005, 8080, 8009) required by Tomcat v6.0 Server at localhost are already in use. The ...
- WCF 采用net.tcp协议实践(转)
概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract),通常,只要这三个因素配置对了,那么,基本 ...