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. MyEclipse 2017 CI 9 发布(附下载)

    挑战全年最低价!MyEclipse线上狂欢继续!火热开启中>> 在进入年底之时,2017 CI 9是我们最大的版本发布之一.在新版本中,我们添加了对Angular 5和TypeScript ...

  2. my.cnf配置优化

    MYSQL服务器my.cnf配置文档详解硬件:内存16G[client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-reha ...

  3. SWIFT用ScrollView加图片制作Banner

    网上参考OBJC写的用ScrollView图片轮播效果,照着画了个,先上效果图: 附上代码: @IBOutlet weak var pc: UIPageControl! @IBOutlet weak ...

  4. mos如何工作参考地址

    https://wenku.baidu.com/view/c118c3fb360cba1aa811da9d.html?qq-pf-to=pcqq.c2c

  5. Loj 2008 小凸想跑步

    Loj 2008 小凸想跑步 \(S(P,p_0,p_1)<S(P,p_i,p_{i+1})\) 这个约束条件对于 \(P_x,P_y\) 是线性的,即将面积用向量叉积表示,暴力拆开,可得到 \ ...

  6. LOJ2540. 「PKUWC2018」随机算法【概率期望DP+状压DP】

    LINK 思路 首先在加入几个点之后所有的点都只有三种状态 一个是在独立集中,一个是和独立集联通,还有一个是没有被访问过 然后前两个状态是可以压缩起来的 因为我们只需要记录下当前独立集大小和是否被访问 ...

  7. 最小生成树--prim+优先队列优化模板

    prim+优先队列模板: #include<stdio.h> //大概要这些头文件 #include<string.h> #include<queue> #incl ...

  8. gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)

    gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...

  9. LG1397 [NOI2013]矩阵游戏

    题意 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的元素,则F[i][ ...

  10. test20181006 投票

    题意 分析 考场30分 枚举大小为k的子集的算法终于用上了. 时间复杂度 \[O\left(\binom{n}{k} \cdot \binom {k}{\frac{k}{2}} \cdot k\rig ...