PAT1003 Emergency (25)(Dijkstra算法)

題目

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 length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) – the number of cities (and the cities are numbered from 0 to N-1), M – the number of roads, C1 and C2 – the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output

2 4

分析題目

題目要求的是最短路徑,以及點(diǎn)權(quán)值的加權(quán)丁溅,用Dijkstra算法

學(xué)習(xí)Dijkstra算法

此處參考了從杰的透徹理解地杰斯特算法

圖1

如圖1探遵,

步驟 集合S 集合U
1 選入A
S={A(0)}
以A為中間點(diǎn)開始找
U = {B(6), C(3), D(∞), E(∞), F(∞)
2 選入C
S = {A(0), C(3)}
以A->C開始找涯穷,若為最短路徑則更新U
U = {B(5), D(6), E(7), F(∞)}
3 選入B
S = {A(0), C(3), B(5)} 以A-B開始找藏雏,若為最短路徑則更新U
U = {D(6), E(7), F(∞)}
4 S = {A(0), C(3), B(5), D(6)} U = {E(7), F(9)}
5 S = {A(0), C(3), B(5), D(6), E(7)} U = F(9)
6 S = {A(0), C(3), B(5), D(6), E(7), F(9)} U 集合為空

按最短路徑長(zhǎng)度的遞增次序依次把第二組的頂點(diǎn)加入S中。在加入的過程中掘殴,總保持從源點(diǎn)v到S中各頂點(diǎn)的最短路徑長(zhǎng)度不大于從源點(diǎn)v到U中任何頂點(diǎn)的最短路徑長(zhǎng)度

代碼

#include <iostream>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int e[510][510], weight[510], dis[510], num[510], w[510];
bool visit[510];
const int inf = 99999999;
int main() {
    scanf("%d%d%d%d", &n, &m, &c1, &c2);
    for(int i = 0; i < n; i++)
        scanf("%d", &weight[i]);
    fill(e[0], e[0] + 510 * 510, inf);
    fill(dis, dis + 510, inf);
    int a, b, c;
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d", &a, &b, &c);
        e[a][b] = e[b][a] = c;
    }
    dis[c1] = 0;
    w[c1] = weight[c1];
    num[c1] = 1;
    for(int i = 0; i < n; i++) {
        int u = -1, minn = inf;
        for(int j = 0; j < n; j++) {
            if(visit[j] == false && dis[j] < minn) {
                u = j;
                minn = dis[j];
            }
        }
        if(u == -1) break;
        visit[u] = true;
        for(int v = 0; v < n; v++) {
            if(visit[v] == false && e[u][v] != inf) {
                if(dis[u] + e[u][v] < dis[v]) {
                    dis[v] = dis[u] + e[u][v];
                    num[v] = num[u];
                    w[v] = w[u] + weight[v];
                } else if(dis[u] + e[u][v] == dis[v]) {
                    num[v] = num[v] + num[u];
                    if(w[u] + weight[v] > w[v])
                        w[v] = w[u] + weight[v];
                }
            }
        }
    }
    printf("%d %d", num[c2], w[c2]);
    return 0;
}

參考代碼:日吹柳神

總結(jié)

  1. 使用fill的時(shí)候起意,注意是fill一維矩陣還是二維矩陣
int dis[500];
int E[500][500];
fill(dis,dis+500,inf); //對(duì)一維矩陣
fill(E[0],E[0]+500*500,inf);//對(duì)二維矩陣
  1. 注意fill和memset區(qū)別
    其中fill能用的范圍更廣服爷,而memset一般用于初始化char數(shù)組获诈、string心褐,如果用于初始化int數(shù)組的話只能填入0或1
    具體用法:fill和memset區(qū)別
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市逗爹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌挟冠,老刑警劉巖袍睡,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異控淡,居然都是意外死亡止潘,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門涧狮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來么夫,“玉大人,你說我怎么就攤上這事档痪。” “怎么了拜银?”我有些...
    開封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵遭垛,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我锯仪,道長(zhǎng),這世上最難降的妖魔是什么小腊? 我笑而不...
    開封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮本缠,結(jié)果婚禮上入问,老公的妹妹穿的比我還像新娘。我一直安慰自己芬失,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開白布租漂。 她就那樣靜靜地躺著颊糜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪芭析。 梳的紋絲不亂的頭發(fā)上吞瞪,一...
    開封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天芍秆,我揣著相機(jī)與錄音,去河邊找鬼妖啥。 笑死,一個(gè)胖子當(dāng)著我的面吹牛荆虱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播诉位,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼菜枷,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了岳瞭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤稚瘾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后孟抗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體钻心,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年摊沉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了痒给。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡尼斧,死狀恐怖试吁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情熄捍,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布缚柏,位于F島的核電站碟贾,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏袱耽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一怜浅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧恶座,春花似錦、人聲如沸自点。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽溅潜。三九已至,卻和暖如春粗仓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背借浊。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工萝招, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人曙蒸。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓母赵,卻偏偏與公主長(zhǎng)得像具滴,于是被迫代替她去往敵國(guó)和親凹嘲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子构韵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容