注意这是一个有向图! 多起点,一终点 反过来,看成一个起点,多个终点,找最短路 因为是有向图 所以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 多起点一终点的更多相关文章

  1. hdu 2066 多起点 多终点

    多起点 多终点 无向图 结点的个数要自己求 Sample Input6 2 3 //边数 起点数 终点数1 3 5 //u v w1 4 72 8 123 8 44 9 129 10 21 2 //起 ...

  2. Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)

    Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  3. HDU——2612Find a way(多起点多终点BFS)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. HDU - 2680 最短路 spfa 模板

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...

  5. 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 ( ...

  6. HDU2066一个人的旅行---(多起点多终点最短路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memo ...

  7. dump 验证实例恢复的起点和终点

    什么时候会产生实例恢复呢?当你数据库服务器异常断电,重启数据库就会发生实例恢复.实例恢复是由数据库自动完成的,无须DBA的干涉.当然这里有个前提条件:数据文件. 在线日志文件.控制文件不得有损坏. 我 ...

  8. 【百度地图API】让用户选择起点和终点的驾车导航

    原文:[百度地图API]让用户选择起点和终点的驾车导航 摘要: 如果用户搜索“从机场到火车站”,使用驾车导航DrivingRoute会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...

  9. Coolest Ski Route-不定起点和终点----在有向变的情况下---求最长路

    这题最开始给你了N个点,M条边,边是单向边,问不指定起点和终点,最长路是什么??? 脑补一下,不定起点和终点的最短路,用弗洛伊德算法搞一搞,但是...那个垃圾算法的复杂度是N^3的,但是这个算法的M高 ...

随机推荐

  1. hp电脑重装win7 64位 后 所有软件都装不上问题【转】

    hp 电脑重装后 所有软件都装不上问题 装了近100来次机,第一次遇到这样的. bug描述: 新笔记本刚装了纯净版的64位旗舰版win7,想装软件,就弹出已停止工作.比如装火狐浏览器,弹出火狐浏览器网 ...

  2. Centos7更改yum镜像源

    1. 备份本地yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak 2.获取阿里yum源配置文 ...

  3. 006、容器 What、Why、How(2018-12-21 周五)

    参考https://www.cnblogs.com/CloudMan6/p/6751516.html   What - 什么是容器?       容器是一种轻量级.可移植.自包含的软件打包技术,是应用 ...

  4. webuploader

    https://www.cnblogs.com/study-fanzeng/p/8930939.html http://fex.baidu.com/webuploader/doc/index.html ...

  5. EasyUI Combobox 设置默认值

    /** *绑定运营商,设置默认值, 显示CMCC, 传值1 */ $('#operatingId').combobox({ url:'data_url', valueField:'id', textF ...

  6. LINQ to SQL 实现 CASE WHEN THEN 语句

    Ø  前言 没有什么特别的,只是觉得 LINQ 的功能其实还是蛮强大的,所以简单记录下,算是工作笔记吧,有可能还能帮助到其他同学呢^_^. Ø  下面主要使用了 C# 三元运算符实现实现 SQL 中的 ...

  7. 使用jQuery实现返回顶部功能

    <p id="back-to-top"><a href="#top"><span></span>返回顶部< ...

  8. PXC中的GTIDs

    基本环境:PXC 5.7.19 Row+Gtid,3节点 一.Galera GTID vs MySQL GTID 1.1.Galera GTID vs MySQL GTID Both kinds of ...

  9. mongodb系列~ mongodb慢语句(3)

    简介: 关于mongodb慢日志是如何收集 一 mongodb慢日志的开启 1 直接设置参数,不重启服务:db.setProfilingLevel(1) 2 添加启动参数,重启服务:添加profile ...

  10. mongodb系列~mongodb慢语句(2)

    一简介:今天遇到一个慢日志的排查和解决过程 二 版本:3.0.6 三 架构:分片集群 四 具体过程 1 程序响应很慢,具体日志寻找定点sql(mongodb慢日志记录在log日志里) awk '$NF ...