最短路径 3790 (2011-3-6 10:59)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Total Submission(s): 223    Accepted Submission(s): 40

//一次AC

//在基本的Dijkstra算法上加了个对费用的判断的条件

#include<stdio.h>
#define MAX  1001
#define INF 0x7FFFFFF
struct NODE{
int r;
int f; 
}graph[MAX][MAX];    
bool hash[MAX];
int path[MAX];
int cath[MAX];
int N,M;
int min(int a,int b){
 return a<b?a:b;
}
int Dijkstra ( int beg , int end )
{
    path[beg] = 0;
    cath[beg] = 0; 
    hash[beg] = false;
    while ( beg != end )
    {
            int m = INF, temp ,ca=INF;
            for ( int i = 1; i <= N; ++ i )
            {
                  if ( graph[beg][i].r != INF ){
                       path[i] = min ( path[i], path[beg] + graph[beg][i].r );
        cath[i] = path[i]==path[beg] + graph[beg][i].r ? cath[beg]+ graph[beg][i].f : cath[i];
      }
                  if ( (m > path[i] || (m!=INF && m == path[i] && ca>cath[i]))&& hash[i])//
                  {
                       m = path[i];
                       ca = cath[i];
                       temp = i; 
                  }           
            }
            beg = temp;
            if ( m == INF )
                 break;
            hash[beg] = false;
    }
    return path[end]; 
}
int main ()
{
    while ( scanf ( "%d%d", &N, &M ) != EOF )
    {
      if(N==0 && M==0)
       break ;
            for ( int i = 0; i != MAX; ++ i )
            {
                  hash[i] = true;
                  path[i] = INF;
                  cath[i] = INF;
                  for ( int j = 0; j != MAX; ++ j )
                  {
                        graph[i][j].r=graph[i][j].r= INF;   
      graph[i][j].f=graph[i][j].f= INF;     
                  }
            } 
            for ( int i = 0; i != M; ++ i )
            {
                  int c1,c2,cost,money;
                  scanf ( "%d%d%d%d",&c1, &c2, &cost , &money);
                  if ( cost < graph[c1][c2].r || (cost == graph[c1][c2].r && money < graph[c1][c2].f)){
                       graph[c1][c2].r = graph[c2][c1].r = cost; 
                       graph[c1][c2].f = graph[c2][c1].f = money; 
                  }
            }
            int beg,end;
            scanf ( "%d%d",&beg, &end );
            printf("%d",Dijkstra ( beg,end ));
            printf(" %d\n",cath[end]);
        //    printf("cath[1]=%d cath[2]=%d cath[3]=%d\n",cath[1],cath[2],cath[3]);
        //    printf("path[1]=%d path[2]=%d path[3]=%d\n",path[1],path[2],path[3]);
    }
    return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值