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的更多相关文章
随机推荐
- Too Much Money
Too Much Money time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- [转]java static final 初始化
http://tanbing1986411.blog.163.com/blog/static/7259798220103610224434/ java static final 初始化 1.stati ...
- Ubuntu不可以ping百度,但是可以ping通其ip
原来也安装过几个ubuntu系统,但是今天遇到一个很奇怪的情况,明明ifconfig显示已经获取了网络上的ip,但是浏览器就是打不开网页,百思不得其解.因为这几天用ping用多了,所以进行了以下尝试: ...
- Linux设置某软件开机自动启动的方法
方法一 将启动命令写到系统启动时会自动调用的脚本中 echo "/usr/local/apache2/bin/apachectl start" >> /etc/rc.d ...
- RedHat Enterprise Linux AS4&5 安装gcc过程
三.Gcc安装方法(redhat 4): 一.安装步骤 1.使用which gcc命令查看gcc是否安装安装 2.如若没有安装则下载如下安装包,所需安装包如下 一共需要拷贝以下五个安装包: binut ...
- 用phpstudy搭建dedecms网站验证码出不来解决方案
验证码图片不显示,这应该是很多站长朋友们最长遇到的一个问题,本地测试明明好好的,为什么传上空间或者服务器上验证码就无法显示了呢,春哥分析这可能是由于没有加载gd库扩展所引起的,那么怎么解决呢?由于引起 ...
- svn log操作
查看当前文件夹的最近N次提交记录 svn update; svn log --limit <N> -v 含义是:查询最近N次提交记录的详细信息,包括版本号,提交文件列表,log信息 对比某 ...
- Nginx配置proxy_pass【转载】
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 下面四种 ...
- check_partition_aft_merge.sql
spool ./05_check_partition_aft_merge.log @/tmp/rda/chk_freets set echo on feedback on set pagesize 4 ...
- hudson--ant编写记录
最近配置Hudson---持续集成工具,重点是ant的编写. 环境:Ubuntu 虚拟机 hudson系统设置里面jdk ant路径也是Ubuntu里文件路径如:/home/test/java/ant ...