此题中起点有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. Java同步的三种实现方式

    1.使用synchronized关键字修饰类或者代码块: 2.使用Volatile关键字修饰变量: 3.在类中加入重入锁 举例子:多个线程在处理一个共享变量的时候,就会出现线程安全问题.(相当于多个窗 ...

  2. PHP第一节课

    基础语法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  3. SDWC2017游记

    果然我还是那么弱啊.——$Mingqi_H.$ Day -1 下午五点半回家.然而并没有什么事情可做.依旧是下载$Magical\,Mirai$,找一个黄油存起来. emmm...本来是打算去开发区那 ...

  4. CentOS 7.2 安装Python3.5.2

    系统环境: CentOS 7.2 x86_64 安装相关包 (1)# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sq ...

  5. vue cli 平稳升级webapck4

    webpack4 released 已经有一段时间了,插件系统趋于平稳,适逢对webpack3的打包速度很不满意,因此决定将当前在做的项目进行升级,正好也实践一下webpack4. 新特性 0配置 应 ...

  6. 【3】Django创建第一个项目

    天地所以能长且久者,以其不自生,故能长生. --老子<道德经> 写在前面:Django在学习的过程中,我们会参考官方文档,从两部分进行讲解,第一部分主要是一个入门项目的搭建开发,第二部分是 ...

  7. OO第四单元总结——查询UML类图 暨 OO课程总结

    一.本单元两次作业的架构设计总结 作业一.UML类图查询 1. 统计信息图 2. 复杂度分析 基本复杂度(Essential Complexity (ev(G)).模块设计复杂度(Module Des ...

  8. NLP问题特征表达基础 - 语言模型(Language Model)发展演化历程讨论

    1. NLP问题简介 0x1:NLP问题都包括哪些内涵 人们对真实世界的感知被成为感知世界,而人们用语言表达出自己的感知视为文本数据.那么反过来,NLP,或者更精确地表达为文本挖掘,则是从文本数据出发 ...

  9. eclipsephp的环境设置

    因需要研究开发opencart的插件模块,需要一套开发php的环境.这类程序就环境配置就够折腾吐血.自认为最简单的方法: 1.下载easyphp安装,这个玩意简单快捷傻瓜.米是5.38: 2.去ecl ...

  10. (23)Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】

    [Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring bo ...