hdu 2066 多起点 多终点

多起点 多终点 无向图 结点的个数要自己求

Sample Input
6 2 3 //边数 起点数 终点数
1 3 5 //u v w
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2 //起点
8 9 10 //终点

Sample Output
9

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 const int MAXN=1010;
10 const int INF=0x3f3f3f3f;
11 int n ;
12 bool vis[MAXN];
13 int cost[MAXN][MAXN] ;
14 int lowcost[MAXN] ;
15 int pre[MAXN];
16 void Dijkstra(int beg)
17 {
18     for(int i=0;i<n;i++)
19     {
20         lowcost[i]=INF;vis[i]=false;pre[i]=-1;
21     }
22     lowcost[beg]=0;
23     for(int j=0;j<n;j++)
24     {
25         int k=-1;
26         int Min=INF;
27         for(int i=0;i<n;i++)
28             if(!vis[i]&&lowcost[i]<Min)
29             {
30                 Min=lowcost[i];
31                 k=i;
32             }
33             if(k==-1)break;
34             vis[k]=true;
35             for(int i=0;i<n;i++)
36                 if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
37                 {
38                     lowcost[i]=lowcost[k]+cost[k][i];
39                         pre[i]=k;
40                 }
41     }
42 }
43
44 int main ()
45 {
46     //freopen("in.txt","r",stdin) ;
47     int m , s , e;
48     while (scanf("%d %d %d" ,  &m , &s , &e) !=EOF)
49     {
50         int u , v , w ;
51         int i , j , t = -1 ;
52         int a[MAXN] ;
53         int b[MAXN] ;
54         memset(cost, INF, sizeof(cost));
55         while(m--)
56         {
57             scanf("%d %d %d" , &u , &v , &w) ;
58             if (w < cost[u-1][v-1] )
59             {
60                 cost[u-1][v-1] = w ;
61                 cost[v-1][u-1] = w ;
62             }
63
64             if (u > t)
65                 t = u ;
66             if (v > t)
67                 t = v ;
68         }
69         n = t ;
70         for (i = 0 ; i < s ; i++)
71             scanf("%d" , &a[i]) ;
72         for (i = 0 ; i < e ; i++)
73             scanf("%d" , &b[i]) ;
74         int ans = INF ;
75         for (i = 0 ; i < s ; i++)
76         {
77             Dijkstra(a[i]-1) ;
78             for (j = 0 ; j < e ; j++)
79             {
80                 if (lowcost[b[j]-1] < ans)
81                     ans = lowcost[b[j]-1] ;
82             }
83
84         }
85         printf("%d\n" , ans) ;
86     }
87
88     return 0 ;
89 }

时间: 2024-10-22 17:54:21

hdu 2066 多起点 多终点的相关文章

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

题目: A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 709 Accepted Submission(s): 348   Problem Description There is a strange lift.The lift can stop can at every floor as you want, a

(hdu step 4.3.1)Tempter of the Bone(在特定的时间约束下,判断是否能够从起点达到终点)

题目: Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1134 Accepted Submission(s): 379   Problem Description The doggie found a bone in an ancient maze, which fascinated him a lo

HDU 2680 (多起点一个终点最短路)

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

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

Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1347    Accepted Submission(s): 305 Problem Description You need walking from vertex S to vertex T in a graph. If you remove one vert

(hdu step 3.2.1)Max Sum(简单dp:求最大子序列和、起点、终点)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1390 Accepted Submission(s): 542   Problem Descrip

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

http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 36107    Accepted Submission(s): 12313 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,

hdu 2066 一个人的旅行 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目意思:给出T条路,和草儿家相邻的城市编号,以及草儿想去的地方的编号.问从草儿家到达草儿想去的地方的最短时间是多少. 一开始自己写的只能处理单边出发的情况,对于以下这幅图,只能处理箭头所示的方向,不能向下,于是不知道为什么出现了百年难得一遇的Runtime Error(ACCESS_VIOLATION)! 10 2 31 3 53 8 42 5 25 8 31 4 74 9 129 10 2

hdu 2066 一个人的旅行(Dijkstra)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段

HDU 2066 一个人的旅行(Dijstra)

一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18671    Accepted Submission(s): 6505 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰