http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2493

 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int maxn=;
const int maxm=maxn*maxn;
const long long INF=1LL<<;
int vis[maxn],head[maxn];
int n,m,cnt = ;
struct node
{
int u,v;
long long w;
int next; } edge[maxm]; inline void add(int u,int v,long long w)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} void spfa(int s, long long dis[])
{ queue<int>q;
q.push(s);
vis[s] = ;
dis[s] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for (int j = head[u]; j!=-; j= edge[j].next)
{
int v = edge[j].v;
long long w = edge[j].w;
if (dis[u]+w < dis[v])
{
dis[v] = dis[u]+w;
q.push(v);
vis[v] = ;
}
}
} }
void init()
{ }
int main()
{
int u,v;
long long w;
long long dis1[maxn],dis2[maxn];
while(~scanf("%d %d",&n,&m))
{
for (int i = ; i <= n; i++)
{
vis[i] = ;
dis1[i] = INF;
dis2[i] = INF;
head[i] = -;
}
cnt = ;
for (int i = ; i < m; i++)
{
scanf("%d %d %lld",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int s,e;
long long min=INF;
scanf("%d %d",&s,&e);
spfa(s,dis1);//求出起点到所有点的最短路径
spfa(e,dis2);//求出终点到所有点的最短路径
for (int i = ; i < cnt; i++)//遍历所有的边
{
u = edge[i].u;
v = edge[i].v;
w = edge[i].w;
if (dis1[u]+dis2[v]+w/ < min)//S->u+e->v+w/2即为改变后的权值,找出最小的
min = dis1[u]+dis2[v]+w/;
}
if(min==INF)
{
puts("No solution");
continue;
}
printf("%lld\n",min);
}
return ;
}

Constructing Roads(spfa)的更多相关文章

  1. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  2. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  3. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  4. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. Constructing Roads (MST)

    Constructing Roads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  6. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  7. hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  8. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  9. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

随机推荐

  1. js的hex转base64

    if (!window.atob) { var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 ...

  2. animation与transition区别

    transition: 过渡属性 过渡所需要时间 过渡动画函数 过渡延迟时间:默认值分别为:all 0 ease 0 1.局限性: 1)只能设置一个属性 2)需要伪类/事件触发才执行 3)只能设置动画 ...

  3. Python 内置函数 day4

    import random s = 'abczfg' st= {3,4,9,1,8} print(dir(random))#打印模块内的方法,输出模块/变量可以调用的方法 print(dir(s))# ...

  4. linux常用命令--ubuntu

    linux 操作系统 一.linux 操作系统概述 二.安装linux系统 三.linux系统环境 ubuntu,默认有6个命令交互通道和一个图形界面交互通道,默认进入到的是图形界面通道 命令交互模式 ...

  5. The Morning after Halloween uva1601

    这道题思路还是比较清晰的,建图加bfs或双向bfs,其实后者比前者少了将近一半的时间.. 建图可以把某一点所拥有邻接点长度(数目)记录在数组0这个位置,因为这道题使用vector会超时. #inclu ...

  6. Java POI Excel 导入导出

    这个东西很容易懂,不是特别难,难就难在一些复杂的计算和Excel格式的调整上. 近期写了一个小列子,放上来便于以后使用. POI.jar下载地址:http://mirror.bit.edu.cn/ap ...

  7. F - Many Moves

    F - Many Moves Time limit : 2sec / Memory limit : 256MB Score : 900 points Problem Statement There a ...

  8. hdu_1012_u Calculate e_201310121519

    u Calculate eTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. 使用JConsole观察分析Java程序的运行(转)

    一.JConsole是什么 从Java 5开始 引入了JConsole.JConsole是一个内置Java性能分析器,可以从命令行或在GUI shell中运行.您可以轻松地使用JConsole(或者, ...

  10. ORA-12547错误

    http://www.linuxidc.com/Linux/2013-03/81330p3.htm << good http://www.verydemo.com/demo_c158_i6 ...