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列表[]中括号

    names = ['jack', 'rose', 'tom', 'jerry', 'jerry'] print(names) print(names[0]) names[0] = 'adam' # 改 ...

  2. 一张图告诉你为何必须学Python?

    互联网行业的薪资高.发展前景好,已经是人尽皆知的事了.越来越多的人对编程有了兴趣,想通过加入大公司实现人生逆袭,我们身边也涌现出了一些从零学习.变身大神的励志故事. 但更多的人还是选择观望:有人觉得编 ...

  3. tp 缓存,前台提速

  4. centos 7安装tomcat

    1.下载安装包 http://tomcat.apache.org/download-80.cgi 2.安装tomcat 注:安装前需要安装jdk环境 #解压 [root@localhost soft] ...

  5. S3TC IAP15F2K61S2点亮一个发光二极管keil和stc-isp软件操作

    1.安装破解软件 2.打开STC-ISP,找到头文件,选择保存文件 3.找到keil的安装目录,keil/C51/INC 并保存 4.在桌面新建文件夹 5.打开keil 6.找到在桌面上新建的文件夹 ...

  6. TJU Problem 1644 Reverse Text

    注意: int N; cin >> N; cin.ignore(); 同于 int N; scanf("%d\n",&N); 另:关于 cin 与 scanf: ...

  7. [LeetCode&Python] Problem 821. Shortest Distance to a Character

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  8. PR5

    修改字幕的两种方式

  9. 实习第二天-今年第一场雨-方法的重载(马上想到println()函数和abs(函数))

    在C语言中 调用函数abs()返回一个整数的绝对值, fabs(返回一个单精度浮点型的绝对值)若要返回这些数的绝对值,则必须记住这些函数的名字 java可以用方法的重载:即是方法名必须相同,参数必须不 ...

  10. 干了2个月java开发最深的体会

    综上为JQuery中ajax的操作,感觉使用多了,和form表单的处理还是非常相似的,只不过实现的功能不一样罢了.学习编程,其实就是学习对数据的流转处理,也即是程序的运行流程,如何从前台获取,传输到服 ...