题意:给定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的更多相关文章

随机推荐

  1. Hibernate 系列教程5-双向多对一

    主要讲解inverse和cascade的用法 cascade定义的是关系两端对象到对象的级联关系: 而inverse定义的是关系和对象的级联关系(管理外键的值). inverse 属性默认是false ...

  2. Temporary exceptions can be configured via your app's Info.plist file.

    报错: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure ...

  3. 【转载】关于SetWindowOrgEx、SetViewportOrgEx、SetViewportExtEx 和SetWindowExtEx 详解

    关于SetWindowOrgEx.SetViewportOrgEx.SetViewportExtEx 和SetWindowExtEx 详解 1.  SetWindowOrgEx是设置窗口的原点坐标. ...

  4. PARTITION BY 和 group by

    sum()   over   (PARTITION   BY   ...)   是一个分析函数.   他执行的效果跟普通的sum   ...group   by   ...不一样,它计算组中表达式的累 ...

  5. POJ2299--树状数组求逆序数

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  6. android 图片拍照图片旋转的处理方式

    第一种:String str=path; /** * 读取图片属性:旋转的角度 * * @param path * 图片绝对路径 * @return degree旋转的角度 */ private vo ...

  7. Windows编译安装mod_wsgi,配合使用Django+Apahce

    编译环境: 均是32位版本 Microsoft Visual Studio 10.0 Microsoft SDKs v7.1 Apache2.4 Python3.4 mod_wsgi-4.5.0 要求 ...

  8. View与ViewGroup有什么区别?

    百度知道:http://zhidao.baidu.com/link?url=B5MFOzDlww8soYqr5CL5FldH4sXD6eumS1XTRn8XEh8gu4mKjQdPkJSLIBt7u_ ...

  9. hdu_5145_NPY and girls(莫队算法+组合)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 题意:给你n,m,共有n个女孩,标号为1—n,n个数xi表示第ith个女孩在第xi个教室,然后下 ...

  10. nested query for "pat2" table

    mysql> select t.appln_id, t.filing_date, t.appln_kind, t.people, GROUP_CONCAT(pu.publn_kind) from ...