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的更多相关文章
随机推荐
- jquery 简单的栏目切换
<style> ul{ list-style:none; padding:0px; margin:0px;} #nav_box{ width:502px; height:402px; ov ...
- Drawcli分析
当前环境:windows7 32位旗舰版.VS2010旗舰版 Drawcli介绍: Drawcli是VS2010中的一个示例程序,能够进行简单的绘图操作,例如线.矩形.圆角矩形.多边形等,位于VS安装 ...
- MaterialEditText 控件学习
这个视图原始框架地址:https://github.com/rengwuxian/MaterialEditText 指导手册:http://www.rengwuxian.com/post/materi ...
- HDU 2671 Can't be easier
简单的几何题目 点(a,b)关于直线Ax+By+C=1对称点的公式 #include<cstdio> #include<cstring> #include<cmath&g ...
- js实现类似于add(1)(2)(3)调用方式的方法
群里有人说实现类似add(1)(2)(3)调用方式的方法,结果马上有人回答: var add = function(a){ return function(b){ return function(c) ...
- jquery指定div右键事件
<div class="nav_list_item">需要淡季右键的div</div> 我们要 给这个div注册鼠标右键的时候需要先禁用该div的网页右键菜 ...
- English--Computer System
A: Hey, Bill, Can you tell what's wrong with my computer? I can't move the mouse, I can's user the k ...
- VS2010 C#调用C++ DLL文件
http://www.soaspx.com/dotnet/csharp/csharp_20110406_7469.html http://www.cnblogs.com/warensoft/archi ...
- 几种在shell命令行中过滤adb logcat输出的方法
我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adblog ...
- DWR 整合之Struts2.3.16
DWR 能够和任何框架结合. DWR 和 Struts 整合有 2 个层次.最基础的层次就是同时使用这两个框架,这是非常容易的,但是这样就不允许在 DWR 和 Struts 之间共享 Action 了 ...