POJ3268Dijkstra
题意:给定n个点,m条边,求所有顶点中到顶点x的来回最短距离
分析:考虑到数据范围,选用Dijkstra,用Floyd会超时
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int INF=<<;
const int maxn=;
struct edge{
int to,cost;
};
typedef pair<int,int> P;
vector<edge> g[maxn];
int d[maxn];
int dp[maxn][maxn];
int n,m,x;
void dijkstra(int s)
{
priority_queue<P, vector<P>, greater<P> > que;
fill(d,d+n+,INF);
d[s]=;
que.push(P(,s));
while(!que.empty()){
P p=que.top(); que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<g[v].size();i++){
edge e=g[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}
int main()
{
while(cin>>n>>m>>x)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dp[i][j]=INF;
for(int i=;i<m;i++)
{
edge e;
int a,b,num;
scanf("%d%d%d",&a,&b,&num);
e.to=b,e.cost=num;
g[a].push_back(e);
}
for(int i=;i<=n;i++){
dijkstra(i);
for(int j=;j<=n;j++)
dp[i][j]=d[j];
}
int cnt;
int mx=;
for(int i=;i<=n;i++){
cnt=;
cnt+=dp[i][x];
cnt+=dp[x][i];
mx=max(cnt,mx);
}
cout<<mx<<endl;
}
return ;
}
POJ3268Dijkstra的更多相关文章
随机推荐
- oracle多种导入导出数据方法
dmp格式: 1.dmp格式的导出可以通过客户端工具(PL/SQL)操作来完成,通过菜单栏---->Tools---->Export Tables,然后设置勾选相应参数即可,rows代表是 ...
- TextureView+SurfaceTexture+OpenGL ES来播放视频(二)
引自:http://www.jianshu.com/p/b2d949ab1a1a 在使用OpenGL ES 绘制前,我先概括下接下来要做的工作:我先借用一个博主kiffa举的的一个栗子,我觉得说的恰到 ...
- CodeForces 709B Checkpoints 模拟
题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. #include&l ...
- 最近关于ACM训练与算法的总结
到了大四以后越来越意识到基础知识的重要性,很多高屋建瓴的观点与想法都是建立在坚实的基础之上的, 招式只有在强劲的内力下才能发挥最大的作用,曾经有段时间我有这样的想法:我们出去以后和其他 ...
- cfdiv2/c/找规律
题目连接 £:若n<4,NO: £:若n==4,特判,n==5,特判. £:若n>=6,用2-4组成24,1和5和6组成零,即可. #include <set> #includ ...
- 关于glibc中的res_init()函数
/* * Set up default settings. If the configuration file exist, the values * there will have precede ...
- js框架——angular.js
这是一个前端用的框架,使用简单.详细介绍这里就不写了,主要介绍其语法和作用—— 1. 声明一个angular 如果想要使用一个angular代码,则需要在[想要使用angular的范围内写上ng-ap ...
- PAT乙1001
我脑洞大了..... 以为是个找规律..... 原来只是模拟..... 我相信肯定是有规律的..... 但是我就是不愿意了..... #include<cstdio> #include&l ...
- h5移动端设计页面
@京东设计中心 :去年JDC出了不少优秀的武媚娘…不,H5呢,大家都很拼,同时当然也积累了一些经验和教训,今天结合咱们的实战案例,从字体,排版,动效,音效,适配性,想法这几个方面好好聊一聊关于H5的设 ...
- 标准与扩展ACL实验
一标准访问控制列表实验: 实验拓扑: 实验目的:掌握标准与扩展ACL的配置 实验要求:拒绝R1到R3的所有流量 实验步骤: 步骤1 按如上拓扑做好底层配置,并检测相邻设备之间的连通性 步骤2起静态路由 ...