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. Spring MVC和Spring Data JPA之获取数据表数据放在List集合,显示在JSP页面

    涉及到很多xml配置没写:只写具体实现的所有类 1.实体类 对应数据表SYS_SBGL, 主键是SBBM,主键是自动生成的uuid 数据表内容如下(有图有真相): package com.jinhet ...

  2. 手把手教你搭建一个Elasticsearch集群

    一.为何要搭建 Elasticsearch 集群 凡事都要讲究个为什么.在搭建集群之前,我们首先先问一句,为什么我们需要搭建集群?它有什么优势呢? (1)高可用性 Elasticsearch 作为一个 ...

  3. 【数据库】MFC ODBC(四)

    7.滚动记录 CRecordset提供了几个成员函数用来在记录集中滚动.当用这些函数滚动到一个新记录时,框架会自动地把新记录的内容拷贝到域数据成员中. void MoveNext( ); //前进一个 ...

  4. 微软IE团队发布《逃离XP》浏览器小游戏

    微软IE团队今天发布了一个有趣的小游戏<逃离Windows XP>,这款游戏可以工作在所有现代浏览器下,简单的街机风格可以让你尽情对XP施展破坏欲.微软发布XP及提供支持服务已经史无前例地 ...

  5. Windows8连接网络后自动弹出Bing解决方法

    Windows8 网络连接速度很快( ADSL ),但是连接之后总是会打开 Bing,这是很烦人的一件事,因为你连接网络可能并不想打开浏览器,甚至,你讨厌 Bing. 我也一直被这个问题困扰了很久,用 ...

  6. SharePoint场管理-PowerShell(一)

    1. 查看场配置信息 Get-SPFarmConfig 2. 设置场配置信息 Set-SPFarmConfig –ASPScriptOptimizationEnabled:$true –DataFor ...

  7. Python与系统的交互方式

    本节内容 os与commands模块 subprocess模块 subprocess.Popen类 总结 我们几乎可以在任何操作系统上通过命令行指令与操作系统进行交互,比如Linux平台下的shell ...

  8. Iview同步异步验证

    直接上代码了: html: <Form ref="termForm" :model="currentData" :rules="ruleTerm ...

  9. NBUT 1218 You are my brother 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB Little A gets to know a new friend, Little B, recently. On ...

  10. art.dialog 使用说明

    Js代码 2. 传入HTMLElement   备注:1.元素不是复制而是完整移动到对话框中,所以原有的事件与属性都将会保留 2.如果隐藏元素被传入到对话框,会设置display:block属性显示该 ...