最短路径问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4184 Accepted Submission(s): 1249
(1<n<=1000, 0<m<100000, s != t)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <queue>
#define N 1003
using namespace std;
struct node
{
int d,p;
};
node map[N][N];
bool b[N];
int n,s,t;
void dijk()
{
memset(b,0,sizeof(b));
b[s]=1;
int i,j,min,m=n;
while(--m)
{
min=1000000;
for(i=1;i<=n;i++)
if(!b[i])
{
if(map[s][i].d<min)
{
min=map[s][i].d;
j=i;
}else if(min<1000000&&map[s][i].d==min)
{
if(map[s][i].p<map[s][j].p)
j=i;
}
}
b[j]=1;
for(i=1;i<=n;i++)
if(!b[i])
{
if(map[s][i].d>map[s][j].d+map[j][i].d)
{
map[s][i].d=map[s][j].d+map[j][i].d;
map[s][i].p=map[s][j].p+map[j][i].p;
}
else if(map[s][i].d==map[s][j].d+map[j][i].d)
{
if(map[s][i].p>map[s][j].p+map[j][i].p)
map[s][i].p=map[s][j].p+map[j][i].p;
}
}
}
printf("%d %d\n",map[s][t].d,map[s][t].p);
}
int main()
{
int m;
int i,j,d,p;
while(scanf("%d%d",&n,&m),n||m)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j].d=1000000;
while(m--)
{
scanf("%d%d%d%d",&i,&j,&d,&p);
if(map[i][j].d>d)
{
map[j][i].d=map[i][j].d=d;
map[j][i].p=map[i][j].p=p;
}
else if(map[i][j].d==d&&map[i][j].p>p)
{
map[j][i].p=map[i][j].p=p;
}
}
scanf("%d%d",&s,&t);
dijk();
}
return 0;
}