一、题面

POJ3268

二、分析

该题的意思就是给定了一个由每个节点代表农场的有向图,选定一个农场X办party,其余农场的都要去,每个农场的cow都走最短路,走的时间最久的cow耗时多少。

了解题意后,最开始想的是直接用floyd,但是复杂度已经到10的9次方了。这题比较特殊的一点就是无论是回来还是去都与X这个点有关,所以,当我们思考去求X到其他点的最短路径时,即根据输入的图求,其实就是在求返回的时长。如果我们把输入的图的路径的方向都反一下,即$a-b$变成$b-a$,这样相当于就是求其他所有点到X的时长了,两个对应相加求最大值就是最终的结果了。求的时候用dijkstra即可。

三、AC代码

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; typedef pair<int, int> P;
const int MAXN = 1e3;
const int INF = 0x3fffffff;
int map[MAXN+][MAXN+], map2[MAXN+][MAXN+];
int dist[MAXN+], dist2[MAXN+];
int N, M, X; void Max(int &a, int b)
{
if(a < b)
a = b;
} void dijkstra(int s, int graph[][MAXN+], int d[])
{
priority_queue<P, vector<P>, greater<P> > pq;
fill(d, d+N, INF);
d[s] = ;
pq.push(P(, s));
while(!pq.empty())
{
P t = pq.top();
pq.pop();
if(d[t.second] < t.first)
continue;
for(int i = ; i < N; i++)
{
if(d[i] > d[t.second] + graph[t.second][i])
{
d[i] = d[t.second] + graph[t.second][i];
pq.push(P(d[i], i));
}
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b, c, Ans;
scanf("%d %d %d", &N, &M, &X);
for(int i = ; i < N; i++)
{ for(int j = ; j < N; j++)
{
map[i][j] = INF;
map2[j][i] = INF;
}
map[i][i] = ;
map2[i][i] = ;
}
for(int i = ; i < M; i++)
{
scanf("%d %d %d", &a, &b, &c);
map[a-][b-] = c;
map2[b-][a-] = c;
}
dijkstra(X-, map, dist);
dijkstra(X-, map2, dist2);
Ans = ;
for(int i = ; i < N; i++)
{
Max(Ans, dist[i] + dist2[i]);
}
printf("%d\n", Ans);
}

POJ_3268 Silver Cow Party 【最短路】的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路

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

  2. POJ3268 Silver Cow Party —— 最短路

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

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

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

  4. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  5. (poj)3268 Silver Cow Party 最短路

    Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...

  6. poj 3268 Silver Cow Party(最短路dijkstra)

    描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...

  7. TZOJ 1693 Silver Cow Party(最短路+思维)

    描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big ...

  8. B - B Silver Cow Party (最短路+转置)

    有n个农场,编号1~N,农场里奶牛将去X号农场.这N个农场之间有M条单向路(注意),通过第i条路将需要花费Ti单位时间.选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间 ...

  9. Silver Cow Party(最短路,好题)

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

随机推荐

  1. code3728 联合权值

    一开始暴搜,超时3个点... 后来看了题解: 首先,两个点的距离为2当且仅当它们都与一个点直接相连 反过来说,一个点所有的出边的终点都是互相距离2的(最大值可以依靠这个方法,前向星处理的时候将每个点的 ...

  2. redis集群部署及常用的操作命令(上)

    简单说下自己测试搭建简单的redis集群的大体步骤: 1.首先你的有6个redis(官方说最少6个,3master,3slave),可以先在一台机器上搭建,搭建到多台上应该只需要改变启动命令即可(可能 ...

  3. 一些..C#知识点总结

    C# 知识点汇总 (其实C#与Java多少有区别,对于咱这个幼儿园大班生来说) 1.认识C#程序 (1)namespqce关键字 namespqce(命名空间)是C#组织代码的方式,它的作用类似于Ja ...

  4. 第08章 ElasticSearch Java API

    本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...

  5. Cocosd-x-2.2.2 & VS2012 & Eclipse 开发环境搭建

    1.安装软件: 1.1 安装JDK(JDK1.7.0_51) JAVA_HOME C:\Program Files\Java\jdk1..0_51 CLASSPATH .;%JAVA_HOME%\li ...

  6. NIOS II SOPC系统自定义IP常见知识点总结

    封装IP1.将写好的Verilog代码添加在Quartus工程中,IP目录下,(如果没有,自己建一个)2.打开Qsys工具,选择New Component3.name和Display name输入合理 ...

  7. xml构建

    <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=346252320&site=qq ...

  8. MVC4 View 的呈现

    一 ActionResult: 1. EmptyResult: Action方法返回的ActionResult对象被ActionInvoker 调用以实现对当前请求的响应,不论Action方法是否具有 ...

  9. iOS AppStore个人开发者账号申请

    一.申请Apple Developer账号 1.注册App ID 1.打开苹果开发者网页,选择Account,注册Apple ID.   2.填写注册信息 3.地区选择China,填写好验证码,点击C ...

  10. MacOS安装使用Node.js

    1. 到官网https://nodejs.org/zh-cn/download/下载,选择Macintosh Installer, 如下: 2. 按预设的下一步,Node.js版本为v6.10.0, ...