把p[i][j],写成g[i][j],结果wa了十次!!我可以再坑点吗?!
这题要注意重边情况的处理,还有重边时cost的更新(不过据说数据没有卡到cost更新上)。还有就是在dijkstra上更新时要注意分==和<的情况!
再附上一组数据,方便检查:
5 7
1 2 5 5
2 3 4 5
1 3 4 6
3 4 2 2
3 5 4 7
4 5 2 4
1 3 4 4
1 5
0 0
答案是8 10;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1005;
int g[maxn][maxn];
int vis[maxn],dis[maxn],p[maxn][maxn],cost[maxn];
void init(int n)
{
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
p[i][j]=g[i][j]=INF;
for(int i=0;i<=n;i++)p[i][i]=g[i][i]=0;
}
void dijkstra(int from,int to,int n)
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
dis[i]=g[from][i];
cost[i]=p[from][i];
}
cost[from]=dis[from]=0;
vis[from]=1;
for(int i=1;i<n;i++){
int x,MIN=INF;
for(int y=1;y<=n;y++)
if(!vis[y]&&dis[y]<MIN)
MIN=dis[x=y];
//if(MIN==INF)return ;
vis[x]=1;
for(int y=1;y<=n;y++){
if(!vis[y]&&g[x][y]<INF){
int len=dis[x]+g[x][y],w=cost[x]+p[x][y];
if(dis[y]==len){
if(cost[y]>w)cost[y]=w;
}
else if(dis[y]>len){
cost[y]=w;
dis[y]=len;
}
}
}
}
printf("%d %d\n",dis[to],cost[to]);
}
int main()
{
//freopen("int.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m)&&(n||m)){
init(n);
int x,y,w,l;
for(int i=0;i<m;i++){
scanf("%d%d%d%d",&x,&y,&w,&l);
if(w<g[x][y]){
g[y][x]=g[x][y]=w;
p[y][x]=p[x][y]=l;
}
else if(w==g[x][y]){
if(l<p[x][y]){
p[y][x]=p[x][y]=l;
}
}
}
scanf("%d%d",&x,&y);
dijkstra(x,y,n);
}
}