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;
    }
  • 相关阅读:
    UE4 Hello World 创建第一个UE4工程
    集团企业数据信息系统建设方案
    Ubuntu_ROS中应用kinect v2笔记
    电力企业计量生产运行系统总体解决方案
    电力企业信息化建设解决方案之计量生产分析系统
    BQ24296充电管理芯片使用过程中的注意事项
    微信测试号开发之四 获取access_token和jsapi_ticket
    微信测试号开发之五 自定义菜单
    微信测试号开发之六 图灵自动回复文本消息
    微信测试号开发之七 获取用户地理位置
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3295252.html
Copyright © 2011-2022 走看看