此题中起点有1000个,边有20000条。用链式前向星建图,再枚举起点用SPFA的话,超时了。(按理说,两千万的复杂度应该没超吧。不过一般说计算机计算速度 1~10 千万次/秒。也许拿最烂的计算机来卡时间)

  有一个技巧,加一个超级源点。也就是加一个点,使得该点连通所有的起点,并且边的权值为0。这个技巧应用蛮多的。网络流、最小树形图都有题目这样做。

 

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N = , M=;
const int INF = 0x3f3f3f3f;
struct node
{
int to, w, next;
};
node edge[M];
int head[N], dist[N], outq[N];
bool vis[N];
int tot;
bool SPFA(int s, int n )
{
int i,k;
for(i=;i<=n;i++) dist[i]=INF;
memset(vis,,sizeof(vis));
memset(outq,,sizeof(outq));
queue<int > q;
while(!q.empty()) q.pop();
vis[s]=;
dist[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=;
outq[u]++;
if(outq[u]>n) return ;
k=head[u];
while(k>=)
{
if(dist[edge[k].to]-edge[k].w>dist[u])
{
dist[edge[k].to]=dist[u]+edge[k].w;
if(!vis[edge[k].to])
{
vis[edge[k].to]=;
q.push(edge[k].to);
}
}
k=edge[k].next;
}
}
return ;
}
void addedge(int i,int j,int w)
{
edge[tot].to=j;
edge[tot].w=w;
edge[tot].next=head[i];
head[i]=tot++;
}
void init()
{
tot=;
memset(head,-,sizeof(head));
}
int main()
{
//freopen("test.txt","r",stdin);
int i,j,k,n,m,t,s;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
init();
while(m--)
{
scanf("%d%d%d",&i,&j,&k);
addedge(i,j,k);
}
scanf("%d",&k);
for(i=;i<k;i++)
{
scanf("%d",&s);
addedge(n+,s,);
}
SPFA(n+,n+);
if(dist[t]==INF) printf("-1\n");
else printf("%d\n",dist[t]);
}
return ;
}

hdu2680 Choose the best route 最短路(多源转单源)的更多相关文章

  1. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. hdu-2680 Choose the best route(最短路)

    题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  3. HDU2680 Choose the best route 2017-04-12 18:47 28人阅读 评论(0) 收藏

    Choose the best route Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  4. hdu2680 choose the best route

    题目 题意:给定一个有向图,多个起点,一个终点,求起点到终点的最短路. 这道题TLE了好多次,两侧次的对比主要在于对起点的处理上,法一:最开始是采用的hdu2066--一个人的旅行,这道题的方法做的, ...

  5. HDU-2680 Choose the best route 单向边+反向dijkstra

    https://vjudge.net/problem/HDU-2680 题意:以起始点 终点 长度 给出一个图,已知可以从w个起点出发,求从任一起点到同一个终点s的最短路径.注意是单向边.m<1 ...

  6. 最短路之SPFA(单源)HDU 2544

    #include <iostream> #include <queue> #include <algorithm> #define MAXLEN 1005 #def ...

  7. 最短路之SPFA(单源)HDU 2066

    #include "iostream" #include "cstdio" #include "queue" #include <cs ...

  8. 最短路之Dijkstra(单源)HDU 2544

    #include <iostream> using namespace std; ; ][]; ]; int middist; ]; void dijkstra(int n,int m) ...

  9. 最短路之SPFA(单源)HDU 1317

    #include <iostream> #include<cstdio> #include<cstring> #include<cmath> #incl ...

随机推荐

  1. luoguP1725 琪露诺 单调队列

    DP 方程:$f[i]=max(f[j])+v[i]$ 转移范围:$i-r<=j<=i-l$ 由此我们得知,每次只有 $[i-r,i-l]$ 部分的 $f$ 值对新更新的答案会有贡献. 故 ...

  2. 【转载】IDEA的这八条配置你一定要改!

    引言 坦白说,我很少写这种操作类型的文章.因为这种文章没啥新意,大家操作步骤肯定是一样的.然而,我答应了我的同事小阳,给她出一篇!毕竟人家打算从Eclipse转IDEA了,于是以示鼓励,写一篇给她!那 ...

  3. [系统资源]port range

    ip_local_port_range 端口范围 sysctl Linux中有限定端口的使用范围,如果我要为我的程序预留某些端口,那么我需要控制这个端口范围, 本文主要描述如何去修改端口范围. /pr ...

  4. 分布式系列文章 —— 从 ACID 到 CAP / BASE

    转自:https://mp.weixin.qq.com/s?amp;mid=2652037708&__biz=MzI0NDI0MTgyOA%3D%3D&idx=1&chksm= ...

  5. Ubuntu Server下docker实战 02: docker进阶配置

    在上一篇文章里<Ubuntu Server下docker实战 01: 安装docker>,我们已经把docker安装起来了,并运行了一个hello-world 这一篇,我们继续讲进阶配置. ...

  6. 如何制作一个类似jquery插件的vue插件

    vue拿来写插件,会不会太那啥? 请跟我念,"不会,符合业务需求才是你的老板最想要的." 如何封装一个可以全局调用的vue插件 其原理其实相当简单,通过new Vue(vueCom ...

  7. C#学习笔记_04_流程控制

    04_流程控制 程序的执行结构: 顺序结构 分支结构 循环结构 可以通过某些特定的控制语句来控制代码的执行结构 分支流程控制 if else 基本语法 可以只有if没有else,但是不能没有if只有e ...

  8. pl/sql developer 自动输入替换 光标自动定位

    pl/sql developer 自动输入替换 工具->首选项->用户界面->编辑器->自动替换,自己定义一些规则,然后输入key,点击tab或者空格,就可以进行替换了: SL ...

  9. boost中的有用工具assign和uuid

    assign assign重载'+'=和','实现连续赋值 assign不仅支持所有8个STL标准容器(vector.string.deque.list.set.multiset.map.multim ...

  10. sass基础教程

    1. 使用变量; $highlight-color: #F90; .selected { border: 1px solid $highlight-color; } //编译后 .selected { ...