题目大意:

给你一个有向图,一个起点集合,一个终点,求最短路。。。。

解题思路:

1.自己多加一个超级源点,把起点集合连接到超级源点上,然后将起点与超级源点的集合的路径长度设为0,这样就称为一个n+1个点的单源最短路算法。

 #include <stdio.h>
#include <string.h> int map[][];
int n; int Dijkstra(int s,int e){
bool done[];
int d[];
memset(done,,sizeof(done));
for(int i = ;i <= n;i++)
d[i] = (i == s ? : );
for(int i = ;i <= n;i++){//最多执行n+1次操作
int minx,minn = ;
for(int j = ;j <= n;j++)//先找到d[]最小的点
if(!done[j] && d[j] < minn){
minn = d[j];
minx = j;
}
done[minx] = ;//将该点加入集合
if(minx == e)
return d[minx];
for(int j = ;j <= n;j++){//再更新所有的d[]
if(!done[j] && d[minx] + map[minx][j] < d[j]){
d[j] = d[minx] + map[minx][j];
}
}
}
return -;//如果没有找到到终点的路径,返回-1
} int main(){
int m,s;
int i,j,k;
int p,q,t;
int w,ww,ans; while(scanf("%d%d%d",&n,&m,&s) != EOF){
ans = ;
for(i = ;i < ;i++){
for(j = ;j < ;j++){
if(i == j)
map[i][j] = ;
else
map[i][j] = ;
}
} while(m--){
scanf("%d%d%d",&p,&q,&t);
if(t < map[p][q])//可能两站间存在多条线路取短的那条路
map[p][q] = t;
}
scanf("%d",&w);
while(w--){
scanf("%d",&ww);
map[][ww] = ;
}
ans = Dijkstra(,s);//巧妙之处,加入超级源点0
if(ans == -)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}

Choose the best route

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.

Input

There are several test cases.
Each case begins with three integers
n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number
of bus stations in this city and m stands for the number of directed
ways between bus stations .(Maybe there are several ways between two bus
stations .) s stands for the bus station that near Kiki’s friend’s
home.
Then follow m lines ,each line contains three integers p , q , t
(0<t<=1000). means from station p to station q there is a way and
it will costs t minutes .
Then a line with an integer
w(0<w<n), means the number of stations Kiki can take at the
beginning. Then follows w integers stands for these stations.

Output

The output contains one line for each data set : the least time Kiki
needs to spend ,if it’s impossible to find such a route ,just output
“-1”.

Sample Input

5 8 5
1 2 2
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

Author

dandelion

HDOJ 2680 Dijkstra的更多相关文章

  1. hdoj 1874 dijkstra

    在做PAT的甲1003,思考DFS和图什么的,时间紧张直接去看柳神(日后上传柳神的C++版本)的订阅,得知是dijkstra,转去用hdoj 1874练手,写了两天,终于调出来了 题目链接:http: ...

  2. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

  3. hdu 2680 Choose the best route (dijkstra算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...

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

  5. Choose the best route HDU杭电2680【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  6. hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)这题有点意思

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. hdoj 1874 畅通工程续【dijkstra算法or spfa算法】

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. hdoj 2544 最短路【dijkstra or spfa】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. HDOJ/HDU 2544 最短路---dijkstra算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...

随机推荐

  1. JNI(2)

    JNI(2) 访问字段和方法 JNI允许本地代码访问java 对象的字段和方法. 调用需要两个步骤: 例如调用cls类的f方法, 1. 获取方法ID jmethodID mid = env->G ...

  2. nvarchar and nchar

    Same: 1.store unicode charactor. 2. A charactor is stroed with 2 bytes. Different. 1. nchar  会自动填充数据 ...

  3. UML初了解

    最近项目中有看UML图,很是模糊一些东西,今天查资料,发现这篇博文很好,转载了,也解了不少疑惑. 原文:http://www.uml.org.cn/oobject/201008311.asp 在绘画U ...

  4. 五毛的cocos2d-x学习笔记02-基本项目源码分析

    class AppDelegate : private cocos2d::Application private表示私有继承,cocs2d是一个命名空间.私有继承下,Application类中的pri ...

  5. An update on OS X Code Signing(OS X代码签名)

    There has recently been updates to the OS X code signing process. These updates also affect Qt appli ...

  6. Android Texting(2)Testing Fundamentals 测试基础篇

    Testing Fundamentals The Android testing framework, an integral part of the development environment, ...

  7. db2 xml 转 table【XQuery系列】

    版本号:DB2 Version 9.1 1.创建測试表,初始化数据 create table emp (doc XML);   INSERT INTO EMP VALUES ('<dept bl ...

  8. BZOJ 1597: [Usaco2008 Mar]土地购买( dp + 斜率优化 )

    既然每块都要买, 那么一块土地被另一块包含就可以不考虑. 先按长排序, 去掉不考虑的土地, 剩下的土地长x递增, 宽y递减 dp(v) = min{ dp(p)+xv*yp+1 } 假设dp(v)由i ...

  9. android 判断网络连接的工具类

    package com.way.util; import android.content.Context; import android.net.ConnectivityManager; import ...

  10. XCode 6 出现 no identity found: Command /usr/bin/codesign failed with exit code 1 解决方法汇总

    1, 解决办法,进入开发者账号重建一个 Provisioning Profiles(或配套证书) 文件,把证书添加正确就可以了 (应该是最有效的) 2, 将p12文件重新安装下 3, 在 iPhone ...