zoukankan      html  css  js  c++  java
  • NEFU 644 touring compute 最短路

    touring compute

    Time Limit 1000ms

    Memory Limit 65536K

    description

    The best friends Mr. Li and Mr. Liu are touring in beautiful country M.
        M has n cities and m two-way roads in total. Each road connects two cities with fixed length.We assume that the cost of car traveling on the road is only related to the length of road,the longer road the more money to pay.
        Now,both Mr. Li and Mr. Liu are in the city C,they have chosen to travel separately by the next time.
    Mr. Li chooses city A with beautiful scenery to be next place, Mr. Liu goes to city B with ancient temples.
        You are their friend with clever minds,just tell them how to arrive the target places make total costs of them minimum.
    							

    input

    The input file contains sevearl test cases.The first line of each case are two positive integers n,and m(3 <= n< =5000, 1 <=m <=10000). The cities are named from 1 to n.Three positive integers C, A, B are follwing.Then,m lines are given,each line contains three integers i,j and k,indicating one road between i and j is exists,and should pay cost k by the car.
        Process to the end of file.
    							

    output

    For each test case, first print a line saying "Scenario #p", where p is the number of the test case.Then,if both Mr. Li and Mr. Liu can manage to arrive their cities,output the minimum cost they will spend,otherwise output "Can not reah!", in one line.Print a blank line after each test case, even after the last one.
    							

    sample_input

    4 5
    1 3 4
    1 2 100
    1 3 200
    1 4 300
    2 3 50
    2 4 100
    4 6
    1 3 4
    1 2 100
    1 3 200
    1 4 300
    2 3 50
    2 4 100
    3 4 50
    							

    sample_output

    Scenario #1
    250
    
    Scenario #2
    200

    先分别求出ABC三点到各个点的最短路

    再枚举两人分开的地点,选取最小值

    long long 是个坑..............

    #include <iostream>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    
    const int maxn=55555;
    const int INF=1e9;
    
    struct XNODE
    {
        int to;
        int w;
        int next;
    }edges[maxn];
    
    int n,m;
    int C,A,B;
    
    int edge,node,src;
    long long distA[maxn];
    long long distB[maxn];
    long long distC[maxn];
    int head[maxn];
    bool visit[maxn];
    int outque[maxn];
    
    queue<int>que;
    
    void prepare(int _node)
    {
        node=_node;
        for (int i=0;i<=node;i++) head[i]=-1;
        edge=0;
    }
    
    void addedge(int u,int v,int c)
    {
        edges[edge].w=c;edges[edge].to=v;edges[edge].next=head[u];head[u]=edge++;
        edges[edge].w=c;edges[edge].to=u;edges[edge].next=head[v];head[v]=edge++;
    }
    
    bool SPFA(int src,long long *dist)
    {
        int top;
        for (int i=0;i<=node;i++)
        {
            dist[i]=INF;
        }
        memset(visit,0,sizeof(visit));
        memset(outque,0,sizeof(outque));
        while (!que.empty()) que.pop();
        que.push(src);
        visit[src]=true;
        dist[src]=0;
        while (!que.empty())
        {
            top=que.front();
            que.pop();
            visit[top]=false;
            outque[top]++;
            if (outque[top]>node) return false;
            int k=head[top];
            while (k!=-1)
            {
                if ( dist[edges[k].to]==INF||dist[edges[k].to]>dist[top]+edges[k].w )
                {
                    dist[edges[k].to]=dist[top]+edges[k].w;
                    if (!visit[edges[k].to])
                    {
                        visit[edges[k].to]=true;
                        que.push(edges[k].to);
                    }
                }
                k=edges[k].next;
            }
        }
        return true;
    }
    
    
    int main()
    {
        int cnt=0;
        while (cin>>n>>m)
        {
            cnt++;
            cin>>C>>A>>B;
            prepare(n);
            for (int i=1;i<=m;i++)
            {
                int u,v,c;
                cin>>u>>v>>c;
                addedge(u,v,c);
            }
            SPFA(C,distC);
            SPFA(A,distA);
            SPFA(B,distB);
            long long ans=INF;
            for (int i=1;i<=n;i++)
            {
                ans=min( ans, distC[i]+distA[i]+distB[i] );
            }
            cout<<"Scenario #"<<cnt<<endl;
            if (ans==INF)
            {
                cout<<"Can not go reach!"<<endl;
            }
            else cout<<ans<<endl;
            cout<<endl;
        }
        return 0;
    }
    




  • 相关阅读:
    SpringCloud分布式开发五大神兽
    Spring Cloud 架构 五大神兽的功能
    kafka 基础知识梳理-kafka是一种高吞吐量的分布式发布订阅消息系统
    ETL工具之Kettle的简单使用一(不同数据库之间的数据抽取-转换-加载)
    libjson 编译和使用
    一个用C++写的Json解析与处理库
    DB-library 常用函数
    什么是C++虚函数、虚函数的作用和使用方法
    C++用iconv进行页面字符转换
    QT学习:c++解析html相关
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038469.html
Copyright © 2011-2022 走看看