zoukankan      html  css  js  c++  java
  • Til the Cows Come Home(赶牛回家)

    poj 2387

    题目大意:求出图中最短的路径

    解决:迪杰斯特拉 或者 spfa 在此处用spfa(队列)

    #include <iostream>
    #include <cstdio>
    #include <queue>
    using namespace std;
    const int N=1005;
    const int MAX=0x3f3f3f3f;
    int cost[N][N];
    int dist[N];
    bool inq[N];
    int n;
    void init(int v0)
    {
        for(int i=1;i<=n;i++)
        {
           for(int j=1;j<=n;j++)
           cost[i][j]=MAX;
           dist[i]=MAX;
           inq[i]=false;
        }
        dist[v0]=0;
    }
    void spfa(int v0)
    {//spfa实现部分
        queue<int> q;
        q.push(v0);
        inq[v0]=1;
        while(!q.empty())
        {
            v0=q.front();
            q.pop();
            inq[v0]=0;
            for(int i=1;i<=n;i++)
             if(cost[v0][i]<MAX && dist[v0]+cost[v0][i]<dist[i])
             {
    //有了这个判断,让时间从160ms 到 30ms,一定要判断,否则进队列 的就多了,可以多次进,但不是在队列一个了,还要进
    dist[i]=dist[v0]+cost[v0][i]; if(!inq[i]){ inq[i]=1; q.push(i);} } } } int main() { int t; scanf("%d%d",&t,&n); int a,b,w; init(1); for(int i=0;i<t;i++) { scanf("%d%d%d",&a,&b,&w); if(cost[a][b]>w)cost[b][a]=cost[a][b]=w; } spfa(1); printf("%d\n",dist[n]); system("pause"); return 0; }

  • 相关阅读:
    Spark2 Dataset DataFrame空值null,NaN判断和处理
    Spark2 文件处理和jar包执行
    &与&&, |与||区别
    Scala实现乘法口诀
    Hive desc
    Hive FUNCTIONS函数
    Hive show
    MySQL行列转换拼接
    MySQL字符串连接
    SQLServer之索引简介
  • 原文地址:https://www.cnblogs.com/hpustudent/p/2144313.html
Copyright © 2011-2022 走看看