HDU Today HDU2112

来源:互联网 发布:极客学院 java 编辑:程序博客网 时间:2024/06/03 16:01

题意:大概意思就是求一个图的单源最短路,这样,嗯。

思路:和直接的单源最短路问题不一样的就是输入的不是地点的编号而是字符串,只需要对每个字符串进行编号就好了。

因为地点最多150,所以直接用邻接矩阵,方便。
本来用string+map实现过一个,结果3790ms。。。后来看了改成char数组快了不少,一千多毫秒。
代码:

#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>#include<queue>#include<utility>#include<vector>#include<cmath>#include<set>#include<map>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 200;const int INF = 0x3f3f3f3f;int N;int G[MAXN][MAXN];int start, endd;char stn[MAXN][35];int tot;int d[MAXN];bool vis[MAXN];void Add(char s[]){    for(int i=0; i<tot; i++){        if(strcmp(s, stn[i]) == 0){            return ;        }    }    strcpy(stn[tot++], s);}int GetNum(char s[]){    for(int i=0; i<tot; i++){        if(strcmp(s, stn[i]) == 0){            return i;        }    }}int Dijkstra(int s){    for(int i=0; i<tot; i++) {d[i] = INF; vis[i] = false;}    d[s] = 0;    for(int i=0; i<tot; i++){        int x, m = INF;        for(int j=0; j<tot; j++){            if(!vis[j] && d[j]<m) m = d[x=j];        }        vis[x] = true;        for(int j=0; j<tot; j++){            if(!vis[j] && G[x][j]<INF && d[x]<INF && d[j]>d[x]+G[x][j]){                d[j] = d[x] + G[x][j];            }        }    }}void Init(){    for(int i=0; i<MAXN; i++){        for(int j=0; j<MAXN; j++){            G[i][j] = INF;        }    }    tot = 0;}int main(){    //freopen("in.txt", "r", stdin);    while(scanf("%d", &N)==1 && N!=-1){        Init();        char s[35], e[35];        scanf("%s%s", s, e);        Add(s);        Add(e);        while(N--){            char a[35], b[35];            int c;            scanf("%s%s%d", a, b, &c);            Add(a);            Add(b);            int x = GetNum(a);            int y = GetNum(b);            G[x][y] = c;            G[y][x] = c;        }        Dijkstra(GetNum(s));        printf("%d\n", d[GetNum(e)]<INF? d[GetNum(e)] : -1);    }    return 0;}

用优先权队列实现的Dijkstra,只想说用优先权队列实现自己还是不怎么熟悉。

#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>#include<queue>#include<utility>#include<vector>#include<cmath>#include<set>#include<map>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 200;const int INF = 0x3f3f3f3f;struct Node{    int v, w;    Node(){}    Node(int a, int b): v(a), w(b){}    bool operator < (const Node& n)const{        return w > n.w;    }};int N;int G[MAXN][MAXN];int start, endd;char stn[MAXN][35];int tot;int d[MAXN];bool vis[MAXN];void Add(char s[]){    for(int i=0; i<tot; i++){        if(strcmp(s, stn[i]) == 0){            return ;        }    }    strcpy(stn[tot++], s);}int GetNum(char s[]){    for(int i=0; i<tot; i++){        if(strcmp(s, stn[i]) == 0){            return i;        }    }}int Dijkstra(int s){    for(int i=0; i<tot; i++) {d[i] = INF; vis[i] = false;}    d[s] = 0;    priority_queue<Node> que;    que.push(Node(s, 0));    while(!que.empty()){        Node tmp = que.top(); que.pop();        vis[tmp.v] = true;        for(int i=0; i<tot; i++){            if(!vis[i] && G[tmp.v][i]<INF && d[tmp.v]<INF && d[i]>d[tmp.v]+G[tmp.v][i]){                d[i] = d[tmp.v] + G[tmp.v][i];                que.push(Node(i, d[i]));            }        }    }}void Init(){    for(int i=0; i<MAXN; i++){        for(int j=0; j<MAXN; j++){            G[i][j] = INF;        }    }    tot = 0;}int main(){    //freopen("in.txt", "r", stdin);    while(scanf("%d", &N)==1 && N!=-1){        Init();        char s[35], e[35];        scanf("%s%s", s, e);        Add(s);        Add(e);        while(N--){            char a[35], b[35];            int c;            scanf("%s%s%d", a, b, &c);            Add(a);            Add(b);            int x = GetNum(a);            int y = GetNum(b);            G[x][y] = c;            G[y][x] = c;        }        Dijkstra(GetNum(s));        printf("%d\n", d[GetNum(e)]<INF? d[GetNum(e)] : -1);    }    return 0;}
原创粉丝点击