hdu 2680 多起点一终点
注意这是一个有向图! 多起点,一终点 反过来,看成一个起点,多个终点,找最短路 因为是有向图 所以u->v 要也要反过来成为v->u
Sample Input
5 8 5 //结点数 边数 终点
1 2 2 //u v w
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3 //起点
4 3 4
1 2 3
1 3 4
2 3 2
1
1
Sample Output
1
-1
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int MAXN=;
const int INF=0x3f3f3f3f;
int n ;
bool vis[MAXN];
int cost[MAXN][MAXN] ;
int lowcost[MAXN] ;
int pre[MAXN];
void Dijkstra(int beg)
{
for(int i=;i<n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)break;
vis[k]=true;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
}
} int main ()
{
//freopen("in.txt","r",stdin) ;
int m , s ;
while (scanf("%d %d %d" , &n , &m , &s) !=EOF)
{ int u , v , w ;
int i , j ;
memset(cost, INF, sizeof(cost));
while(m--)
{
scanf("%d%d%d" , &u , &v , &w) ;
if (w < cost[v-][u-]) //防止重边
{
cost[v-][u-] = w ;
}
}
Dijkstra(s-) ;
scanf("%d" , &m) ;
int e ;
int MIN = INF ;
while(m--)
{
scanf("%d" , &e) ;
if (lowcost[e-] < MIN)
MIN = lowcost[e-] ;
} if (MIN != INF)
printf("%d\n" , MIN) ;
else
printf("-1\n") ;
} return ;
}
堆优化
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
struct qnode
{
int v;
int c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
int n ;
void Dijkstra(int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
} int main ()
{
//freopen("in.txt","r",stdin) ;
int m , s ;
while (scanf("%d %d %d" , &n , &m , &s) !=EOF)
{ int u , v , w ;
int i , j ;
for(i=;i<=n;i++)
E[i].clear(); while(m--)
{
scanf("%d%d%d" , &u , &v , &w) ;
addedge(v,u,w) ;
}
Dijkstra(s) ;
scanf("%d" , &m) ;
int e ;
int MIN = INF ;
while(m--)
{
scanf("%d" , &e) ;
if (dist[e] < MIN)
MIN = dist[e] ;
} if (MIN != INF)
printf("%d\n" , MIN) ;
else
printf("-1\n") ;
} return ;
}
hdu 2680 多起点一终点的更多相关文章
- hdu 2066 多起点 多终点
多起点 多终点 无向图 结点的个数要自己求 Sample Input6 2 3 //边数 起点数 终点数1 3 5 //u v w1 4 72 8 123 8 44 9 129 10 21 2 //起 ...
- Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)
Key Vertex Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- HDU——2612Find a way(多起点多终点BFS)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU - 2680 最短路 spfa 模板
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...
- HDU2066一个人的旅行---(多起点多终点最短路径)
http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memo ...
- dump 验证实例恢复的起点和终点
什么时候会产生实例恢复呢?当你数据库服务器异常断电,重启数据库就会发生实例恢复.实例恢复是由数据库自动完成的,无须DBA的干涉.当然这里有个前提条件:数据文件. 在线日志文件.控制文件不得有损坏. 我 ...
- 【百度地图API】让用户选择起点和终点的驾车导航
原文:[百度地图API]让用户选择起点和终点的驾车导航 摘要: 如果用户搜索“从机场到火车站”,使用驾车导航DrivingRoute会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...
- Coolest Ski Route-不定起点和终点----在有向变的情况下---求最长路
这题最开始给你了N个点,M条边,边是单向边,问不指定起点和终点,最长路是什么??? 脑补一下,不定起点和终点的最短路,用弗洛伊德算法搞一搞,但是...那个垃圾算法的复杂度是N^3的,但是这个算法的M高 ...
随机推荐
- JAVA记录-SpringMVC国际化配置
1.搭建SpringMVC框架,不过多阐述 2.spring-mvc.xml加入以下配置: <!-- 国际化资源配置,资源文件绑定器--> <bean id="messag ...
- 在IIS上启用Gzip压缩(HTTP压缩)
一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联网收集整理, 主要来源于中文wiki. 使用Y ...
- C#常用的正则工具类写法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- UVALive 7143 Room Assignment(组合数学+DP)
题目链接 参考自:http://www.cnblogs.com/oyking/p/4508260.html 题意 n个人,其中有k对双胞胎.现有m间房间,每间房间有容量ci问分配房间的方案数. 分析 ...
- 定时器QTimer
import sys from PyQt5.QtCore import QTimer, Qt from PyQt5.QtWidgets import QApplication, QWidget, QP ...
- TCP3次握手和4次挥手及其为什么
TCP 3次握手 客户端向服务器发送一个SYN(包含了SYN,SEQ). 当服务器接收到客户端发过来的SYN时,会向客户端发送一个SYN+ACK的数据包,其实ACK的ack等于上一次发送SYN数据包的 ...
- 列式数据库~clickhouse 数据同步使用
一 简介:进一步了解clickhouse二 数据操 1 单机建表 create TABLE aaa ( id UInt32, uid UInt32, amount Float64, ...
- jquery选择器最后一个,倒数第二个元素
<div> <p>1</p> <p>2</p> <p>3</p> <p>4</p> < ...
- 【BARTS计划】【Tips_Week1】20190331更新
BARTS计划 · Review :每周学习至少一个技术技巧. 一.快捷键 1. 快速批量注释代码的方法:选中需要注释的代码,按 ctrl+/ 二.重要命令行命令 1. 新增文件:git add a. ...
- Linux之定时器与时间管理 【转】
转自:http://blog.chinaunix.net/uid-23228758-id-154820.html 定时器与时间管理: 次,为一秒.一般的情况下编程者不要改变这个值,因为内核编很多代码都 ...