此题中起点有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. null值处理

    一,在实体类的上面加注解 import com.fasterxml.jackson.annotation.JsonInclude @JsonInclude(JsonInclude.Include.NO ...

  2. 【剑指Offer】42、和为S的两个数字

      题目描述:   输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的.   输出描述:   对应每个测试案例,输出两个数, ...

  3. java环境搭建心得

     右击此电脑,点击属性, 在打开的电脑系统对话框里发电机i直接点击左侧导航里的[高级系统设置]在打开的电脑系统属性对话框里直接点击下面的[环境变量] 打开环境变量对话框后,直接点击系统变量下面的新建, ...

  4. 家谱(gen)——洛谷P2814

    #include <iostream> #include <string> #include <map> using namespace std; map < ...

  5. 数位dp题集

    题集见大佬博客 不要62 入门题,检验刚才自己有没有看懂 注意一些细节. 的确挺套路的 #include<bits/stdc++.h> #define REP(i, a, b) for(r ...

  6. springboot启动报:Error creating bean with name 'dataSource' defined in class path resource

    需要在启动类的@EnableAutoConfiguration或@SpringBootApplication中添加exclude = {DataSourceAutoConfiguration.clas ...

  7. 【Codeforces Round #519 by Botan Investments C】 Smallest Word

    [链接] 我是链接,点我呀:) [题意] [题解] 模拟了一两下.. 然后发现. 对于每一个前缀. 组成的新的最小字典序的字符串 要么是s[i]+reverse(前i-1个字符经过操作形成的最大字典序 ...

  8. 05springMVC数据格式化

    数据格式化简介 内建的格式转换器 使用内建格式转换器示例 字段级别的解析/格式化 集成到Spring Web MVC环境 1      数据格式化简介 对属性对象的输入/输出进行格式化,其实是属于“类 ...

  9. ubuntu--Supervisor的简单使用

    安装,这个程序使用python写的 sudo apt-get install supervisor 配置一个你需要的配置文件 //进入 /etc/supervisor/conf.d文件目录,配置一个r ...

  10. 笔记本联想(Lenovo)G40-70M加装内存和SSD固态硬盘

    笔记本联想(Lenovo)G40-70M加装内存和SSD固态硬盘 系列文章: 笔记本电脑提速之加装内存条.SSD固态硬盘.光驱位换SSD固态硬盘 笔记本ThinkPad E430c加装内存和SSD固态 ...