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 ...
随机推荐
- slenium的xpath几种定位方式
练习地址,以下面地址为例: http://www.w3school.com.cn/example/xmle/books.xml 1. 查找book对象 //book #所有的数 //book[1] ...
- Miller-Rabbin 素性测试 和 Pollard_rho整数分解
今天学习一下Miller-Rabbin 素性测试 和 Pollard_rho整数分解. 两者都是概率算法. Miller_Rabbin素性测试是对简单伪素数pseudoprime测试的改进. (ps ...
- POJ-2195 Going Home---KM算法求最小权值匹配(存负边)
题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...
- 【BZOJ4810】[YNOI2017] 由乃的玉米田(莫队+bitset)
点此看题面 大致题意: 给你一段序列,每次询问一段区间内是否存在两个数的差或和或积为\(x\). 莫队算法 看到区间询问+可以离线,首先想到了莫队啊. 但是,在较短的时间内更新信息依然比较难以实现. ...
- 【BZOJ1925】 [SDOI2010] 地精部落(带有一堆性质的动态规划)
点此看题面 大致题意: 问你有多少长度为\(n\)的数列,它当中每个数字要么比旁边两个数字都小,要么比旁边两个数字都大. 性质 这题应该比较显然是一道动态规划题,但刚看到这题时我却无从下手. 其实,了 ...
- 【dp】奶牛家谱 Cow Pedigrees
令人窒息的奶牛题 题目描述 农民约翰准备购买一群新奶牛. 在这个新的奶牛群中, 每一个母亲奶牛都生两个小奶牛.这些奶牛间的关系可以用二叉树来表示.这些二叉树总共有N个节点(3 <= N < ...
- pymysql模块操作数据库及连接报错解决方法
import pymysql sql = "select host,user,password from user" #想要执行的MySQL语句 #sql = 'create da ...
- 在kali上安装谷歌浏览器
在kali上安装谷歌浏览器的时候,遇到了很多问题,经过不懈努力,终于解决,现在把方法总结一下,希望对遇到同样问题的人能有一定帮助.这是给最白的小白参考的,大牛勿喷哈. 首先去这个网站www.googl ...
- Thinkphp5中的Validate验证器的使用
更多笔记: http://note.youdao.com/noteshare?id=e97a5df64888f27d912b3e966b9ec297&sub=web1520841813815 ...
- Ubuntu samba 安装与配置 实现windows和虚拟机中的Ubuntu共享文件
2. 安装sumba服务 sudo apt-get install samba samba-common 这里出现了小问题, Ubuntu上安装samba不能安装的问题,“下列的软件包有不能满足 ...