POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party
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 ≤ X ≤ N). 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
Lines 2..
M+1: Line
i+1 describes road
i with three space-separated integers:
Ai,
Bi, and
Ti. The described road runs from farm
Ai to farm
Bi, requiring
Ti time units to traverse.
Output
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
Hint
#include <set>
#include <map>
#include <stack>
#include <stdio.h>
#include <vector>
#include <utility>
#include<string.h>
#include <queue>
#include <iterator>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int inf = 0x3f3f3f3f;
int n,m,x;
struct edge
{
int u;
int v;
int cost;
} M[]; //存储原始边的信息;
struct N
{
int e; //每条边的权值;
int to; //终点;
};
vector<N> v[]; // 存储每条边的信息,下标为起始点;
int dis[]; // 起点到各个终点的最短距离;
void dijkstra(int s)
{
priority_queue<pair<int ,int>,vector<pair<int,int> >,greater<pair<int,int> > >q; //利用优先队列对里面的边从小到大进行排序;
for(int i = ;i<=;i++) v[i].clear(); //vector 清空;
fill(dis,dis+,inf); //初始化为最大值;
N n2;
for(int i = ;i<=m;i++) //将初始边加入到vector中;
{
n2.e = M[i].cost;
n2.to = M[i].v;
v[M[i].u].push_back(n2);
}
dis[s] = ;
//优化与实现:
q.push(pair<int,int>(,s));
while(!q.empty())
{
pair<int ,int> p = q.top();
q.pop();
int v1 = p.second;
if(dis[v1]<p.first) continue;
for(int i = ;i<v[v1].size();i++)
{
N n1 = v[v1][i];
if(dis[n1.to]>dis[v1]+n1.e)
{
dis[n1.to]=dis[v1]+n1.e;
q.push(pair<int,int>(dis[n1.to],n1.to));
}
}
}
}
int main()
{
int sum[];
while(~scanf("%d %d %d",&n,&m,&x))
{
memset(sum,,sizeof(sum));
for(int i = ;i<=m;i++) scanf("%d %d %d",&M[i].u,&M[i].v,&M[i].cost);
dijkstra(x);
for(int i = ;i<=n;i++) sum[i]+=dis[i];
for(int i = ;i<=n;i++)
{
dijkstra(i);
sum[i] += dis[x];
}
int max1 = ;
for(int i = ;i<=n;i++) if(sum[i]>max1) max1 = sum[i];
printf("%d\n",max1);
}
return ;
}
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。的更多相关文章
- 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 ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- 论overflow滚动的重要性
原理 设置一个块级作用域溢出的效果,只需要在外部块的位置上设置overflow:scroll和height:xx即可. 此时,块级作用域的内容位移超出外部块的位移就会出现滚动条,当内部块滚动时,我们能 ...
- iptables (1) 原理
网上看到这个配置讲解得还比较易懂,就转过来了,大家一起看下,希望对您工作能有所帮助. iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防 ...
- Java后台工程师的3次面试
第一次面试 我面的是一个中小公司,在BOSS直聘上面找的,去之前看了看关于Java的一些基础知识,在牛客网上面看的,也做了一下牛客网的题目.然后跟HR约了一个时间就去面试了.因为第一次面试,一点经验都 ...
- Bootstrap历练实例:响应式标签页
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- shell脚本,awk结合正则来打印文件里面的内容。
文件内容如下:key1abc d key2 1.想得到如下结果: abc d 2.想得到如下结果: key1key2
- java算法面试题:递归算法题2 第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
package com.swift; public class Digui_Return { public static void main(String[] args) { /* * 递归算法题2 ...
- sql注入问题 java中将MySQL的数据库验证秘密加上 ' or '1'= '1 就可以出现万能密码
password的字符串中,加上 ' or '1'= '1 就可以制作出万能密码. 原因如下: 原代码中密码是123456 执行数据库查询语句 实际上执行的SQL语句是: select * from ...
- 1061: [Noi2008]志愿者招募
Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5742 Solved: 3449[Submit][Status][Discuss] Descript ...
- Java 解决IE浏览器下载文件,文件名出现乱码问题
/** * 区分ie 和其他浏览器的下载文件乱码问题 * @param request * @param fileName * @return */ public String getFileName ...
- day 44 前端HTML
前端HTML HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk ...