此题中起点有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. git_仓库

    本地仓库 仓库(repository)可以理解成一个目录,这个目录里面的所有文件都可以被git管理起来,每个文件的修改删除git都能进行跟踪. 创建一个空目录---进入文件下---查看当前路径,当前路 ...

  2. 01 DOS常用命令

    有时候没有可视化窗口,命令行对文件进行操作更方便快捷 cmd 命令弹出 dir 查看当前所在目录下的文件 ctrl+c 退出 \a 显示隐藏文件 cd /改变到根目录 dir /a 显示隐藏文件 di ...

  3. tomcat 热加载设置

    找到tomcat项目的apache-tomcat-8.0.30\conf\context.xml,打开进行编辑,把Context项中加上 reloadable="true" < ...

  4. [luogu2317 HNOI2005] 星际贸易 (dp)

    传送门 Solution 两个dp分开处理, 第一问什么都不考虑直接dp 第二问还有些疑惑,姑且先留坑 Code //By Menteur_Hxy #include <cstdio> #i ...

  5. Linux思维导图之查找命令

    常用查找命令的区别:

  6. netty使用MessageToByteEncoder 自定义协议(四)

    开发应用程序与应用程序之间的通信,程序之前通信 需要定义协议,比如http协议. 首先我们定义一个协议类 package com.liqiang.SimpeEcode; import java.sql ...

  7. hdu 2586 lca在线算法(朴素算法)

    #include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N ...

  8. 0816关于MySQL的审计 init-connect+binlog实现用户操作追踪

    转自:http://blog.sina.com.cn/s/blog_605f5b4f01013xkv.html mysql 用init-connect+binlog实现用户操作追踪 做access 的 ...

  9. php7 使用imagick 的坑

    imagick是一个PHP的扩展,用ImageMagick提供的API来进行图片的创建与修改,不过这些操作已经包装到扩展imagick中去了,最终调用的是ImageMagick提供的API. Imag ...

  10. 手动重启weblogic脚本

    手动重启weblogic脚本 pid=`ps -ef|grep fzjc_Admin_Server|grep -v grep|awk '{print $2}'` echo $pid kill -9 $ ...