PAT A1072 Gas Station(30 分)-------图最短路径---比较难点的题

总结:

1.这道题用了dijstra算法,关键是开始对G1非数字的处理即Gi处理成i+n;我最后一个测试点开始没过就是因为用s.size()判断输入为数字还是G2,但是其实数据n+m是大于99的

代码:

#include
#include
#include
#include
using namespace std;
int G[1100][1100];
int n, m, k, ds;
const int inf = 99999999;
int dis[1100][1100];
int getid(string s)
{
    if (s[0] != 'G'){ int k = stoi(s); return k; }
    else { string p = s.substr(1, s.size() - 1); return stoi(p) + n; }
}
int main()
{
    cin >> n >> m >> k >> ds;
    fill(G[0], G[0] + 1100 * 1100, inf);
    fill(dis[0], dis[0] + 1100 * 1100, inf);
    for (int i = 1; i <= k; i++)
    {
        string pp1, pp2;
        int p1, p2, dist;
        cin >> pp1 >> pp2;
        scanf("%d", &dist);
        p1 = getid(pp1); p2 = getid(pp2);
        if (p1 == p2)continue;
        if (dist ds){ flag = false; break; }
            totalsum +=dis[i][j];
            if (dis[i][j] minlen)
        { 
            minlen = totallen; 
            minsum = totalsum; 
            index = totalindex;
         }
        else if (totallen == minlen&&totalsum

 

你可能感兴趣的:(算法,pat甲级,数据结构学习)