zoj 1456 Minimum Transport Cost 最短路

   书上把这放在了bellman,但我觉得用floyd应该更加好写些

    注意一个坑人的地方,就是测试数据中有自己到自己,输出是单独的n,而不是n-->n

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define maxn 100
using namespace std;
int dis[maxn][maxn],p[maxn],n;
queue<int> q;
int d[maxn];
int near[maxn];
bool inq[maxn];
int way[2][maxn],K[2];
void path(int now,bool f)
{
    if(near[now]==now)return;
    else
    {
        path(near[now],f);
        way[f][K[f]++]=now;
    }
}
bool cmp(int a,int b,int c)//生成路径比较字典序大小
{
    K[0]=K[1]=0;
    path(a,0);
    path(b,1);
    way[0][K[0]++]=c;
    way[1][K[1]++]=c;
    int i;
    for(i=0;way[0][i]+way[1][i];i++)
    {
        if(way[0][i]>way[1][i])return 0;
        if(way[0][i]<way[1][i])return 1;
    }
    return 0;
}
int bellman(int v,int aim)
{
    if(aim==v)return 0;
    while(!q.empty())q.pop();
    for(int i=0;i<=n;i++)
    {
        near[i]=i;
        inq[i]=0;
        d[i]=100000000;
    }
    d[v]=0;q.push(v);
    int now,next,t;
    while(!q.empty())
    {
        now=q.front();q.pop();
        inq[now]=0;
        for(next=1;next<=n;next++)
        {
            if(next==now||next==v)continue;//防止产生环
            if(dis[now][next]!=-1&&(d[next]>(t=d[now]+dis[now][next]+p[now])||(d[next]==t&&cmp(now,near[next],next))))
            {
                d[next]=t;
                near[next]=now;
                if(inq[next])continue;
                q.push(next);
                inq[next]=1;
            }
        }
    }
    return d[aim]-p[v];
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int i,j;
        for(i=1;i<=n;i++)
          for(j=1;j<=n;j++)
            scanf("%d",&dis[i][j]);
        for(i=1;i<=n;i++)
          scanf("%d",&p[i]);
        int v,u,t;
        while(~scanf("%d%d",&v,&u))
        {
            if(v==-1||u==-1)break;
            t=bellman(v,u);
            printf("From %d to %d :\n",v,u);
            printf("Path: %d",v);
            K[0]=0;
            if(u!=v)
            {
              path(u,0);
              for(int i=0;i<K[0];i++)
                printf("-->%d",way[0][i]);
            }
            printf("\nTotal cost : %d\n\n",t);
        }
    }
}
时间: 2024-10-31 15:47:51

zoj 1456 Minimum Transport Cost 最短路的相关文章

HDU 1385 Minimum Transport Cost:最短路,打印字典序路径

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1385 题目大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 分析与总结: 1.   这题的关键在于按照字典序输出路径. 假设有 1---

hdu 1385 Minimum Transport Cost

点击打开链接hdu 1385 思路:最短路+SPFA+路径记录+路径字典序比较 分析: 1 题目要求的单源的最短路,所以可以选择任意一种单源最短路来求解 2 题目还要求在路径和相同情况下要字典序小的,那么就要在更新dis数组的时候进行更新路径,如果是dis[i]>tmp,那么直接更新:如果是dis[i] == tmp的情况下,那么就要求出star->i 和 star->x的路径进行比较,然后判断能否更新. 3 注意询问的时候可能问的是同一个点. 代码: #include<iostr

zoj 2750 Idiomatic Phrases Game 最短路

   一个预处理比较累的最短路,要先生成边,编码是16进制,一开始写成10进制一直WA-- /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstr

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

POJ 1385

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4487    Accepted Submission(s): 1114 Problem Description These are N cities in Spring country. Between each pair of cities

.NET DateTime coding best practices

.NET DateTime coding best practicesAuthor:  Dan Rogers (danro), Microsoft Corporation January 9, 2004 SynopsisWriting programs that store, perform calculations and serialize time values using the .NET DateTime type requires an awareness of the differ

UVa 10954:Add All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1895 原题: Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to wri

UVa 10670:Work Reduction

[链接] UVA:  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1611 poj:     http://poj.org/problem?id=1907 [原题] Paperwork is beginning to pile up on your desk, and tensions at the wo

Code Jam 2010 Round 1A Problem B

Problem B. Make it Smooth Problem You have a one-dimensional array of N pixels. Each pixel has a value, represented by a number between 0 and 255, inclusive. The distance between two pixels is the absolute difference of their numbers. You can perform