LouZhang

导航

HDU4522

http://acm.hdu.edu.cn/showproblem.php?pid=4522

好久没写了

来写一篇吧

#include <cstdio>
#include <cstring>
using namespace std;


int q, n, t, d1, d2, a, b;
char s[1010][10000+10];
int k[1010];
int g[210][210];
int gg[210][210];
const int inf = 99999999;
void make(){
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            g[i][j] = inf;
    for(int i = 0; i < t; i ++){
        /*
        int start = s[i][0] - 48;
        for(int j = 2; s[i][j]; j += 2){
            int end = s[i][j] - 48;
            g[start][end] = k[i];
            start = end;
        }
        */
        int st = 0, ed;
        int f = 0, sum = 0;
        for(int j = 0; s[i][j]; j ++){
            if(s[i][j] >= 48 && s[i][j] <= '9'){
                sum = sum * 10 + s[i][j] - 48;
            }else if(s[i][j] == '+'){
                if(f == 0){
                    g[st][sum] = k[i];
                    st = sum;
                    sum = 0;
                    f = 1;
                }else{
                    ed = sum;
                    sum = 0;
                    g[st][ed] = k[i];
                    st = ed;
                    f = 0;
                }
            }
            //g[st][sum]
        }
        g[st][sum] = k[i];
    }
}

int dist[210];
int vis[210];
int dijk(int flag){
    //for(int i = 1; i )
    if(flag){
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++){
                gg[i][j] = g[i][j];
                if(g[i][j] == 1)
                    gg[i][j] = d2;
                else if(g[i][j] == 0)
                    gg[i][j] = inf;
            }
    }else{
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++){
                gg[i][j] = g[i][j];
                if(g[i][j] == 1 || g[i][j] == 0)
                    gg[i][j] = d1;
            }
    }
    for(int i = 1; i <= n; i ++){
        dist[i] = gg[a][i];
        vis[i] = 0;
    }
    vis[a] = 1;
    dist[a] = 0;
    for(int i = 0; i < n - 1; i ++){
        int min_num = inf, v = -1;
        for(int j = 1; j <= n; j ++){
            if(!vis[j] && dist[j] < min_num){
                v = j;
                min_num = dist[j];
            }
        }
        //printf("%d++++\n", v);
        if(v == -1) break;
        vis[v] = 1;
        for(int j = 1; j <= n; j ++){
            if(!vis[j] && gg[v][j] < inf && dist[v] + gg[v][j] < dist[j]){
                dist[j] = dist[v] + gg[v][j];
            }
        }
    }
    if(dist[b] == inf) return -1;
    return dist[b];
}
int main(){
    //while(~scanf("%d", &q))
        scanf("%d", &q);
    {
        while(q--){
            scanf("%d%d", &n, &t);
            for(int i = 0; i < t; i ++)
                scanf("%s%d", s[i], k+i);
            scanf("%d%d", &d1, &d2);
            scanf("%d%d", &a, &b);
            make();
            int ans1 = dijk(1);
            int ans2 = dijk(0);
            if(ans1 == -1 && ans2 == -1){
                printf("-1\n");
                continue;
            }
            if(ans1 == -1){
                printf("%d\n", ans2);
                continue;
            }
            if(ans2 == -1){
                printf("%d\n", ans1);
                continue;
            }
            printf("%d\n", ans1 < ans2 ? ans1 : ans2);
        }
    }
    return 0;
}

 

posted on 2013-03-27 10:40  louzhang_swk  阅读(304)  评论(0编辑  收藏  举报