这是我的代码
#include<iostream>
#include<fstream>
#include<vector>
#define inf 9999999;
using namespace std;
int main()
{
// ifstream file("da.txt");
// file >> N >> M >> S >>D;
int N,M,S,D;
cin >>N >>M>>S>>D;
int **map,**cost,*dis,*cos,*last,*book;
map = new int*[N];
cost = new int*[N];
dis = new int[N];
cos = new int[N];
last = new int[N];
book = new int[N];
for(int i= 0; i<N; i++)
{
*(map+i) = new int[N];
*(cost+i) = new int[N];
}
for(int i=0; i<N; i++)
{
dis[i]=inf;
cos[i]=0;
last[i]=0;
book[i]=0;
for(int j=0; j<N; j++)
{
map[i][j]=0;
cost[i][j]=0;
}
}
int a, b,c,d;
for(int i = 1; i<=M ;i++)
{
// file >> a >> b >> c >> d;
cin >> a >> b >> c >> d;
map[a][b]=map[b][a] = c;
cost[a][b]=cost[b][a] = d;
}
for(int i = 0; i<N; i++)
{
if(map[S][i]!=0){
dis[i] = map[S][i];
cos[i] = cost[S][i];
}
}
int index;
int mindis=inf;
for(int k =1; k<N; k++)
{
for(int i =0 ;i<N; i++)
{
if(dis[i] && dis[i]<mindis && !book[i])
{
index = i;
mindis = dis[i];
}
}
book[index] = 1;
mindis = inf;
for(int j= 0; j<N; j++)
{
if(j!=S && dis[j]>=dis[index]+map[index][j] && map[index][j])
{
if(dis[j]==dis[index]+map[index][j] && cos[j] < cos[index]+cost[index][j])
continue;
else
{
dis[j] = dis[index]+map[index][j];
last[j] = index;
cos[j] = cos[index]+cost[index][j];
}
}
}
}
vector<int> line;
int P =D;
while(P!=S)
{
line.push_back(P);
P = last[P];
}
cout << S <<" ";
for(int i=line.size()-1; i>=0 ;i--)
cout << line[i]<<" ";
cout << dis[D]<<" ";
cout << cos[D];
}
我看能通过测试的答案和我差不多。。为啥我这个内存就超了?
感谢!