zoukankan      html  css  js  c++  java
  • TOJ3744(Transportation Costs)

    Transportation Costs 分享至QQ空间 去爱问答提问或回答

    Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte
    Total Submit: 129            Accepted: 34

    Description

     

    Minya Konka decided to go to Fuzhou to participate in the ACM regional contest at their own expense.Through the efforts, they got a small amount of financial support from their school, but the school could pay only one of those costs between two stations for them.
    From SWUST to Fujian Normal University which is the contest organizer, there are a lot of transfer station and there are many routes.For example, you can take the bus to Mianyang Railway Station (airport), then take the train (plane) to Fuzhou,and then take a bus or taxi to the Fujian Normal University.
    The school could pay only one of those costs between two stations for them, the others paid by the Minya Konka team members.They want to know what is the minimum cost the need pay.Can you calculate the minimum cost?

     

    Input

     

    There are several test cases.
    In each case,the first line has two integers n(n<=100) and m(m<=500),it means there are n stations and m undirected roads.
    The next m lines, each line has 3 integers u,v,w,it means there is a undirected road between u and v and it cost w.(1<=u,v<=n,0<=w<=100)
    The ID of SWUST is 1,and n is the ID of Fujian Normal University.

     

    Output

     

    If they can not reach the destination output -1, otherwise output the minimum cost.

     

    Sample Input

     

    5 5
    1 2 7
    1 3 3
    3 4 3
    2 5 3
    4 5 3
    

     

    Sample Output

     

    3
    

     

    Source

    SWUST Monthly, 2011.1

     

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <cstring>
    using namespace std;
    const int INF = 0x7fffffff;
    const int maxn = 110;
    
    int gn, gm;
    vector<pair<int, int> > g[maxn+10];
    bool inque[maxn+10];
    queue<int> Q;
    
    void spfa(int s, int d[]) {
        int i;
        for(i = 1; i < maxn; i++) d[i] = INF;
        d[s] = 0;
        while(!Q.empty()) Q.pop();
        Q.push(s);
        inque[s] = true;
        while(!Q.empty()) {
            int u = Q.front();
            Q.pop();
            for(i = 0; i < (int)g[u].size(); i++) {
                int t = g[u][i].first;
                if(d[u] + g[u][i].second < d[t]) {
                    d[t] = d[u] + g[u][i].second;
                    if(!inque[t]) {
                        inque[t] = true;
                        Q.push(t);
                    }
                }
            }
            inque[u] = false;
        }
    }
    
    void init() {
        int i;
        for(i = 1; i <= gn; i++) {
            g[i].clear();
        }
    }
    
    void work(int d1[], int d2[]) {//枚举每一条边.
        int i, j;
        int mindis = INF;
        int x;
        for(i = 1; i <= gn; i++) {
            for(j = 0; j < (int)g[i].size(); j++) {
                x = g[i][j].first;
                if(d1[i] != INF && d2[i] != INF && d1[i] + d2[x] < mindis) {
                    mindis = d1[i] + d2[x];
                }
            }
        }
        if(mindis != INF) {
            printf("%d
    ", mindis);
        }
        else
            printf("-1
    ");
    }
    
    
    int main()
    {
        int i;
        int u, v, w;
        int d1[maxn];
        int d2[maxn];
        pair<int, int> t;
        while(scanf("%d%d", &gn, &gm) != EOF) {
            init(); //清空容器.
            for(i = 1; i <= gm; i++) {
                scanf("%d%d%d", &u, &v, &w);
                t.first = v;
                t.second = w;
                g[u].push_back(t);
                t.first = u;
                t.second = w;
                g[v].push_back(t);
            }
            spfa(1, d1);//求起点到每个顶点的最短路径.
            spfa(gn, d2);//求终点到每个顶点的最短路径.
            work(d1, d2);//枚举每条边,求最小值.
        }
        return 0;
    }
  • 相关阅读:
    【翻译】R 中的设计模式
    CLR 协变、逆变
    设计模式之工厂模式
    c# 浮点数计算问题
    WebAPI参数传值string转bool,int转bool相关问题
    CLR via c# 值类型“不可变”
    C# CLR via 对象内存中堆的存储【类型对象指针、同步块索引】
    c# 关于字段内存排序
    《拖拉一点也无妨》读后感
    .NET CORE 学习笔记之安装EF【Microsoft.EntityFrameworkCore】扩展报错
  • 原文地址:https://www.cnblogs.com/riskyer/p/3294034.html
Copyright © 2011-2022 走看看