国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

1111 Online Map (PAT甲級)

這篇具有很好參考價值的文章主要介紹了1111 Online Map (PAT甲級)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

這道題我讀題不仔細導(dǎo)致踩了個大坑,一個測試點過不了卡了好幾個小時:第二個dijkstra算法中,題目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我卻想當然地認為在fastest path is not unique的時候,判斷標準是最短距離……

#include <cstdio>
#include <vector>
#include <algorithm>
const int MAXN = 501;
const int INF = 999999999;

int N, M, u, v, oneWay, a, b, src, dst, tmp;
int dist[MAXN][MAXN];
int time[MAXN][MAXN];
int path[MAXN];
std::vector<int> d1, t1, t2, path1, path2;

void dijkstra1(int k){
    int minDist, pivot;
    d1.resize(N, INF);
    t1.resize(N, INF);
    std::vector<bool> visited(N);
    std::fill(visited.begin(), visited.end(), false);
    std::fill(path, path + N, -1);
    d1[k] = 0;
    t1[k] = 0;
    for(int i = 0; i < N; ++i){
        pivot = -1;
        minDist = INF;
        for(int j = 0; j < N; ++j){
            if(!visited[j] && d1[j] < minDist){
                pivot = j;
                minDist = d1[j];
            }
        }
        if(pivot == -1){
            return;
        }
        visited[pivot] = true;
        for(int j = 0; j < N; ++j){
            if(!visited[j] && dist[pivot][j] < INF && d1[pivot] + dist[pivot][j] < d1[j]){
                d1[j] = d1[pivot] + dist[pivot][j];
                t1[j] = t1[pivot] + time[pivot][j];
                path[j] = pivot;
            } else if(!visited[j] && dist[pivot][j] < INF && time[pivot][j] < INF
                && d1[pivot] + dist[pivot][j] == d1[j] && t1[pivot] + time[pivot][j] < t1[j]){
                t1[j] = t1[pivot] + time[pivot][j];
                path[j] = pivot;
            }
        }
    }
}

void dijkstra2(int k){
    int minTime, pivot;
    t2.resize(N, INF);
    std::vector<int> intersection(N, INF);
    std::vector<bool> visited(N);
    std::fill(visited.begin(), visited.end(), false);
    std::fill(path, path + N, -1);
    intersection[k] = 0;
    t2[k] = 0;
    for(int i = 0; i < N; ++i){
        pivot = -1;
        minTime = INF;
        for(int j = 0; j < N; ++j){
            if(!visited[j] && t2[j] < minTime){
                pivot = j;
                minTime = t2[j];
            }
        }
        if(pivot == -1){
            return;
        }
        visited[pivot] = true;
        for(int j = 0; j < N; ++j){
            if(!visited[j] && time[pivot][j] < INF && t2[pivot] + time[pivot][j] < t2[j]){
                t2[j] = t2[pivot] + time[pivot][j];
                intersection[j] = intersection[pivot] + 1;
                path[j] = pivot;
            } else if(!visited[j] && time[pivot][j] < INF
                && t2[pivot] + time[pivot][j] == t2[j] && intersection[pivot] + 1 < intersection[j]){
                intersection[j] = intersection[pivot] + 1;
                path[j] = pivot;
            }
        }
    }
}

int main(){
    std::fill(dist[0], dist[0] + MAXN * MAXN, INF);
    std::fill(time[0], time[0] + MAXN * MAXN, INF);
    scanf("%d %d", &N, &M);
    for(int i = 0; i < M; ++i){
        scanf("%d %d %d %d %d", &u, &v, &oneWay, &a, &b);
        dist[u][v] = a;
        time[u][v] = b;
        if(!oneWay){
            dist[v][u] = a;
            time[v][u] = b;
        }
    }
    scanf("%d %d", &src, &dst);
    dijkstra1(src);
    tmp = dst;
    while(path[tmp] != -1){
        path1.push_back(tmp);
        tmp = path[tmp];
    }
    dijkstra2(src);
    tmp = dst;
    while(path[tmp] != -1){
        path2.push_back(tmp);
        tmp = path[tmp];
    }
    if(path1 == path2){
        printf("Distance = %d; Time = %d: %d", d1[dst], t1[dst], src);
        for(int i = path1.size() - 1; i >= 0; --i){
            printf(" -> %d", path1[i]);
        }
    } else{
        printf("Distance = %d: %d", d1[dst], src);
        for(int i = path1.size() - 1; i >= 0; --i){
            printf(" -> %d", path1[i]);
        }
        printf("\nTime = %d: %d", t2[dst], src);
        for(int i = path2.size() - 1; i >= 0; --i){
            printf(" -> %d", path2[i]);
        }
    }
    return 0;
}

題目如下:

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers?N?(2≤N≤500), and?M, being the total number of streets intersections on a map, and the number of streets, respectively. Then?M?lines follow, each describes a street in the format:

V1 V2 one-way length time

where?V1?and?V2?are the indices (from 0 to?N?1) of the two ends of the street;?one-way?is 1 if the street is one-way from?V1?to?V2, or 0 if not;?length?is the length of the street; and?time?is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance?D?in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time?T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:文章來源地址http://www.zghlxwxcb.cn/news/detail-468927.html

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

到了這里,關(guān)于1111 Online Map (PAT甲級)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • pat甲級 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    瀏覽(23)
  • 1028 List Sorting (PAT甲級)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers?N?(≤105) and?C, where?N?is the number of records and?C?is the column that you are supposed to sort the records with. Then?N?lines follow, eac

    2024年02月15日
    瀏覽(16)
  • 1021 Deepest Root (PAT甲級)

    1021. Deepest Root (25)-PAT甲級真題(圖的遍歷,dfs,連通分量的個數(shù))_柳婼的博客-CSDN博客 柳婼的解法在這里,兩次dfs,還是挺好玩的。 我的解法比較暴力,就是先用并查集算連通分量(這個其實還是dfs來算會更方便),如果只有一個連通分量,那deepest root一定在僅有一條arc的

    2024年02月15日
    瀏覽(16)
  • 1114 Family Property (PAT甲級)

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房產(chǎn))info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate. Input Specification: Each input file contains one test case. For each case, the fir

    2024年02月06日
    瀏覽(19)
  • 1072 Gas Station (PAT甲級)

    A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range. Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendatio

    2024年02月09日
    瀏覽(22)
  • 菜鳥記錄PAT甲級1003--Emergency

    菜鳥記錄PAT甲級1003--Emergency

    久違的PAT,由于考研408數(shù)據(jù)結(jié)構(gòu)中有一定需要,同時也是對先前所遺留的競賽遺憾進行一定彌補 ,再次繼續(xù)PAT甲級1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    瀏覽(97)
  • PAT 甲級【1007 Maximum Subsequence Sum】

    本題是考察動態(tài)規(guī)劃與java的快速輸入: max[i]表示第i個結(jié)尾的最大的連續(xù)子串和。b begin[i]表示第[begin[i],i]為最大和的開始位置 超時代碼: 未超時: 能用動態(tài)規(guī)劃解決的問題,需要滿足三個條件:最優(yōu)子結(jié)構(gòu),無后效性和子問題重疊。 具有最優(yōu)子結(jié)構(gòu)也可能是適合用貪心的

    2024年02月08日
    瀏覽(24)
  • 1074 Reversing Linked List (PAT甲級)

    Given a constant?K?and a singly linked list?L, you are supposed to reverse the links of every?K?elements on?L. For example, given?L?being 1→2→3→4→5→6, if?K=3, then you must output 3→2→1→6→5→4; if?K=4, you must output 4→3→2→1→5→6. Input Specification: Each input file contains one test case. For each case, the first line

    2024年02月09日
    瀏覽(15)
  • PAT 甲級1005【1005 Spell It Right】

    PAT 甲級1005【1005 Spell It Right】

    用JAVA可以用BigInteger解決。 ? ? ? 太長不看版:結(jié)尾自取模板…… 高精度計算(Arbitrary-Precision Arithmetic),也被稱作大整數(shù)(bignum)計算,運用了一些算法結(jié)構(gòu)來支持更大整數(shù)間的運算(數(shù)字大小超過語言內(nèi)建整型)。 高精度問題包含很多小的細節(jié),實現(xiàn)上也有很多講究。

    2024年02月08日
    瀏覽(17)
  • 菜鳥記錄:c語言實現(xiàn)PAT甲級1003--Emergency

    菜鳥記錄:c語言實現(xiàn)PAT甲級1003--Emergency

    久違的PAT,由于考研408數(shù)據(jù)結(jié)構(gòu)中有一定需要,同時也是對先前所遺留的競賽遺憾進行一定彌補 ,再次繼續(xù)PAT甲級1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    瀏覽(87)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包