【最短路】 HDU 2722 Here We Go(relians) Again

求最短的时间到达终点  

每条路的长度 2520  给出的是速度

见代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
//inline void print(LL x){printf(LLD, x);puts("");}
//inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}
const int MAXN=1010;
#define typec int
const typec INF=9999999;//防止后面溢出,这个不能太大
bool vis[MAXN];
int pre[MAXN];
int point;
int n,m;
int cost[MAXN][MAXN],lowcost[MAXN];
void Dijkstra()
{
    for(int i=0; i<point; i++)
    {
        lowcost[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    lowcost[0]=0;
    for(int j=0; j<point; j++)
    {
        int k=-1;
        int Min=INF;
        for(int i=0; i<point; i++)
            if(!vis[i]&&lowcost[i]<Min)
            {
                Min=lowcost[i];
                k=i;
            }
        if(k==-1)break;
        vis[k]=true;
        for(int i=0; i<point; i++)
            if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
            {
                lowcost[i]=lowcost[k]+cost[k][i];
                pre[i]=k;
            }
    }
}

void inedge(int x,int y)
{
        int a;
        char c;
        scanf("%d %c",&a,&c);
        if(a==0)
            cost[x][y]=cost[y][x]=INF;
        else
        {
            if(c=='*')
                cost[x][y]=cost[y][x]=2520/a;
            else if(c=='v')
                cost[x][y]=2520/a;
            else if(c=='^')
                cost[y][x]=2520/a;
            else if(c=='>')
                cost[x][y]=2520/a;
            else if(c=='<')
                cost[y][x]=2520/a;
        }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    while(~scanf("%d%d", &n,&m),n+m)
    {
        point=(n+1)*(m+1);
        for(int i=0;i<point;i++)
        for(int j=0;j<point;j++)
            cost[i][j]=INF;
        for(int j=0;j<point;j++)
            cost[j][j]=0;
        for(int i=0;i<n*2+1;i++)
        {
            if(i%2==0)
            {
                for(int j=0;j<m;j++)
                    inedge(i/2*(m+1)+j,i/2*(m+1)+j+1);
            }
            else
            {
                for(int j=0;j<m+1;j++)
                    inedge(i/2*(m+1)+j,(i/2+1)*(m+1)+j);
            }
        }
        Dijkstra();
        if(lowcost[point-1]==INF)
            printf("Holiday\n");
        else printf("%d blips\n",lowcost[point-1]);
    }
    return 0;
}


posted @ 2014-10-31 10:57  kewowlo  阅读(165)  评论(0编辑  收藏  举报