普通网友 2017-08-14 13:08 采纳率: 0%
浏览 1694

求高手解答!做PAT甲级的1030题时出现内存超限的情况

这是我的代码

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

我看能通过测试的答案和我差不多。。为啥我这个内存就超了?
感谢!

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-08-14 16:19
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?