[最短路径] HDU 2066 - 一个人的旅行

来源:互联网 发布:2017年淘宝天猫电商节 编辑:程序博客网 时间:2024/06/05 03:00

题目就是求S个起点到N个终点的的最短路径。

注意重边,Dijkstra做的~

#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <algorithm>#include <iostream>#include <set>#include <map>#include <queue>#include <stack>#include <assert.h>#include <time.h>typedef long long LL;const int INF = 500000001;const double EPS = 1e-9;const double PI = acos(-1.0);using namespace std;int T, S, D, vis[1001], dis[1001], graph[1001][1001];void init(){    for(int i = 1; i <= 1000; i++)    {        for(int j = 1; j <= 1000; j++)        {            if(i == j) graph[i][j] = 0;            else graph[i][j] = INF;        }    }}void Dijkstra(int s){    for(int i = 1; i <= 1000; i++)    {        dis[i] = graph[s][i];    }    memset(vis, -1, sizeof(vis));    vis[s] = 1;    for(int i = 1; i < 1000; i++)    {        int k = -1;        int minn = INF;        for(int j = 1; j <= 1000; j++)        {            if(vis[j] == -1 && minn > dis[j])            {                minn = dis[j];                k = j;            }        }        if(k == -1) break;        vis[k] = 1;        for(int j = 1; j <= 1000; j++)        {            if(vis[j] == -1 && dis[k] + graph[k][j] < dis[j])            {                dis[j] = dis[k] + graph[k][j];            }        }    }}int main(){    #ifdef _1Test        freopen("test0.in", "r", stdin);        freopen("test0.out", "w", stdout);        srand(time(NULL));    #endif    int s[1001], d[1001], ans;    while(~scanf("%d %d %d", &T, &S, &D))    {        ans = INF;        init();        int x, y, val;        for(int i = 0; i < T; i++)        {            scanf("%d %d %d", &x, &y, &val);            graph[x][y] = graph[y][x] = min(graph[x][y], val);        }        for(int i = 0; i < S; i++)        {            scanf("%d", &s[i]);        }        for(int i = 0; i < D; i++)        {            scanf("%d", &d[i]);            Dijkstra(d[i]);            for(int j = 0; j < S; j++)            {                ans = min(ans, dis[s[j]]);            }        }        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击