Silver Cow Party

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem Description

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 ≤ XN). 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?

 
Input
Line 1: Three space-separated integers, respectively:
<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.
 
Output
Line 1: One integer: the maximum of time any one cow
must walk.
 
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
 
Sample Output
10
 

题意:一群牛分别从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 最短路径+矩阵转换的更多相关文章

  1. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  2. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  3. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  4. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵

    Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...

  6. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  7. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  8. poj 3268 Silver Cow Party

                                                                                                       S ...

  9. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

随机推荐

  1. python 安装包查看

    pip freeze可以查看已经安装的python软件包和版本 pip list 也可以

  2. http指定状态码

    Http状态代码 1.指定状态码: setStatus HttpServletResponse的setStatus方法.如果响应的状态代码比较特殊,并且伴有相关的文档内容,那么一定要在用PrintWr ...

  3. Crystal Report Error: Either the Crystal Reports registy key permission are insufficient or the Crystal Reports runtime is not installed correctly

    在64位 Windows 7中水晶报表的错误: Crystal Report Error: Either the Crystal Reports registy key permission are ...

  4. maven多环境配置

    我们在开发项目的时候,往往会有好几个环境.比如开发.预发布(测试).产品,每个环境一般用到配置都不一样,最典型的就是数据库,开发的数据库与产品的数据库肯定是不一样的,如果要多个环境的切换就得改配置,这 ...

  5. 2019.1.10 L223

    Heavy rains that brought additional pollution downstream last year contributed to the first decline ...

  6. DIY微型操作系统(1)—— 开发的准备

    这个连载是根据<30天自制操作系统>这本书所写 只是类似于补充之类的东西,要详细的讲解,还请参照书上的内容 所以,首先我们要感谢作者川合秀实先生!(鞠躬) 为什么我想写这么一个补充的? 因 ...

  7. Python 名称空间和作用域

    a = 10 # lst = [1,2,3,4] # # # 内置函数 print("你好啊,我叫赛利亚") # def chi(): a = 10 b = 20 # # # # ...

  8. Robolectric测试框架使用笔记

    1. 概述 Robolectric(http://robolectric.org/)是一款支持在桌面JVM模拟Android环境的测试框架,通过shadow包下的类来截取view.activity等类 ...

  9. cobbler网络装机

    cobbler网络装机 原理分析 cobbler简介 Cobbler通过将设置和管理一个安装服务器所涉及的任务集中在一起,从而简化了系统配置.相当于Cobbler封装了DHCP.TFTP.XINTED ...

  10. FreeOpcUa compile

    /********************************************************************************* * FreeOpcUa compi ...