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;
}