hdu2680 Choose the best route 最短路(多源转单源)
此题中起点有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 最短路(多源转单源)的更多相关文章
- 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 ...
- hdu-2680 Choose the best route(最短路)
题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 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 ...
- hdu2680 choose the best route
题目 题意:给定一个有向图,多个起点,一个终点,求起点到终点的最短路. 这道题TLE了好多次,两侧次的对比主要在于对起点的处理上,法一:最开始是采用的hdu2066--一个人的旅行,这道题的方法做的, ...
- HDU-2680 Choose the best route 单向边+反向dijkstra
https://vjudge.net/problem/HDU-2680 题意:以起始点 终点 长度 给出一个图,已知可以从w个起点出发,求从任一起点到同一个终点s的最短路径.注意是单向边.m<1 ...
- 最短路之SPFA(单源)HDU 2544
#include <iostream> #include <queue> #include <algorithm> #define MAXLEN 1005 #def ...
- 最短路之SPFA(单源)HDU 2066
#include "iostream" #include "cstdio" #include "queue" #include <cs ...
- 最短路之Dijkstra(单源)HDU 2544
#include <iostream> using namespace std; ; ][]; ]; int middist; ]; void dijkstra(int n,int m) ...
- 最短路之SPFA(单源)HDU 1317
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #incl ...
随机推荐
- Android 性能测试初探(二)
书接前文 Android 性能测试初探(一).上回大体介绍了下在 android 端的性能测试项,现在我们就细节测试项做一些阐述(包括如何自己 DIY 测试). 首先我们来说说启动时间.关于应用的启动 ...
- js数组操作find查找特定值结合es6特性
js数组操作find查找特定值结合es6特性
- Emoji表情处理工具类
import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmojiToString { /** * 将 ...
- lua_自己对“lua函数”知识点的总结
lua_自己对“lua函数”知识点的总结 1.lua函数的定义 --lua中,函数都是function类型的对象.(1)其可以被比较 (2)其可以赋值给一个对象(3)可以传递给函数(4)可以从函数中返 ...
- Lua循环结构while循环、repeat 循环、for循环_学习笔记03
Lua循环结构while循环.repeat 循环.for循环 while语法结构 while 循环条件 do 循环体 end --1.输出1到100 index = do print(index) ...
- PAT 1089. Insert or Merge
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output li ...
- Bootstrap关于表单(二):水平表单
Bootstrap框架默认的表单是垂直显示风格,但很多时候我们需要的水平表单风格(标签居左,表单控件居右) 在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件: 1.在<form ...
- BZOJ 1396 识别子串 (后缀自动机、线段树)
手动博客搬家: 本文发表于20181221 00:58:26, 原地址https://blog.csdn.net/suncongbo/article/details/85150962 嗯,以后博客内容 ...
- 【hihoCoder挑战赛28 A】异或排序
[题目链接]:http://hihocoder.com/problemset/problem/1509 [题意] [题解] 每次找到相邻两个数的二进制形式中; 不同的最高位; 显然S在这一位必然是确定 ...
- 元素类型为 "session-factory" 的内容必须匹配 "(property*,mapping*,(class-cach....解决方法
http://www.cnblogs.com/kisso143/p/3642057.html property必须写在mapping的上面.