1003 Emergency (25 分)

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 Specification:

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, C?1?? 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 c
?1
?? , c
?2
?? 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 C?1?? to C2?? .

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C?1?? and C?2?? , 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

code

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int dis[500],num[500],weight[500],w[500];
    int adj[500][500];
    int visit[500];
    const int inf = 999999;
    int n,m,origin,dest;
    scanf("%d%d%d%d",&n,&m,&origin,&dest);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&weight[i]);
    }
    fill(adj[0],adj[0]+500*500,inf);
    fill(dis,dis+500,inf);
    fill(num,num+500,0);
    int a,b,c;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        adj[a][b] = adj[b][a] = c;
    }
    dis[origin] = 0;
    w[origin] = weight[origin];
    num[origin] = 1;
    for(int i=0;i<n;i++)
    {
        int x=-1, minn=inf;
        for(int j=0;j<n;j++)
        {
            if(!visit[j] && dis[j]<minn) {
                minn = dis[j];
                x = j;
            }
        }
        visit[x] = 1;
        for(int y=0;y<n;y++)
        {
            if(dis[y]>dis[x]+adj[x][y]) {
                dis[y] = dis[x] + adj[x][y];
                num[y] = num[x];
                w[y] = weight[y]+w[x];
            }else if(dis[y] == dis[x]+adj[x][y] && dis[x]!=0) {
                num[y] = num[y] + num[x];
                if(w[x]+weight[y]>w[y]) {
                    w[y] = w[x] + weight[y];
                }
            }
        }
//        printf("%d\n",i);
//        printf("num:");
//        for(int k=0;k<n;k++)  printf("%d ",num[k]);
//        printf("\n");
//        printf("dis:");
//        for(int k=0;k<n;k++)  printf("%d ",dis[k]);
//        printf("\n");
//        printf("w:");
//        for(int k=0;k<n;k++)  printf("%d ",w[k]);
//        printf("\n");
    }
//    for(int i=0;i<n;i++)
//    {
//        for(int j=0;j<n;j++)
//        {
//            printf("%d ",adj[i][j]);
//        }
//        printf("\n");
//    }
    printf("%d %d", num[dest],w[dest]);
    return 0;
}

note

  • Dijkstra 算法
    假設(shè)我們要求從A到F的最短路個數(shù)和最短路上最大的點權(quán)和
example

上圖的鄰接矩陣表示:
adj[6][6] = \left[ \begin{matrix} InF & 1 & 2 &3& 4& InF\\ 1 &InF& InF& InF& InF& 3\\ 2 &InF &InF &1 &InF& 2\\ 3 &InF& 1 &InF &InF&1\\ 4 &InF& InF& InF &InF&1\\ InF &3& 2 &1& 1 &InF \end{matrix}\right]

點權(quán)數(shù)組
weight[6] =\left[\begin{matrix} 1 & 2&3&4& 5&6\end{matrix}\right]

每次迭代找到dis數(shù)組中最小值,并對與該節(jié)點相連的邊進(jìn)行松弛

A B C D E F
w 1 0 0 0 0 0
dis 0 inf inf inf inf inf
num 1 0 0 0 0 0
visit 0 0 0 0 0 0
A B C D E F
w 1 3 4 5 6 0
dis 0 1 2 3 4 inf
num 1 1 1 1 1 0
visit 1 0 0 0 0 0
A B C D E F
w 1 3 4 5 6 9
dis 0 1 2 3 4 4
num 1 1 1 1 1 1
visit 1 1 0 0 0 0
A B C D E F
w 1 3 4 8 6 10
dis 0 1 2 3 4 4
num 1 1 1 2 1 2
visit 1 1 1 0 0 0
A B C D E F
w 1 3 4 8 6 14
dis 0 1 2 3 4 4
num 1 1 1 2 1 4
visit 1 1 1 1 0 0
A B C D E F
w 1 3 4 8 6 14
dis 0 1 2 3 4 4
num 1 1 1 2 1 4
visit 1 1 1 1 1 0
A B C D E F
w 1 3 4 8 6 14
dis 0 1 2 3 4 4
num 1 1 1 2 1 4
visit 1 1 1 1 1 1
  • fill 和 memset
    fill 可以填充任何值藕各,包含在<algorithm>
    對vector:fill(v.begin(), v.end(), -1)
    對一維數(shù)組:fill(arr, arr + 10, 2)
    對二維數(shù)組:fill(adj[0],adj[0]+500*500,inf);
    memset按照字節(jié)進(jìn)行填充臊岸,一般用來填充char型數(shù)組,也經(jīng)常用于填充int型的全0或全-1操作蕉饼,包含在<cstring>
    對數(shù)組:memset(a, 0, sizeof(a))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末虐杯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子昧港,更是在濱河造成了極大的恐慌擎椰,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件创肥,死亡現(xiàn)場離奇詭異达舒,居然都是意外死亡,警方通過查閱死者的電腦和手機叹侄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門巩搏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人趾代,你說我怎么就攤上這事贯底。” “怎么了撒强?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵禽捆,是天一觀的道長笙什。 經(jīng)常有香客問我,道長胚想,這世上最難降的妖魔是什么琐凭? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮浊服,結(jié)果婚禮上淘正,老公的妹妹穿的比我還像新娘。我一直安慰自己臼闻,他們只是感情好鸿吆,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著述呐,像睡著了一般惩淳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上乓搬,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天思犁,我揣著相機與錄音,去河邊找鬼进肯。 笑死激蹲,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的江掩。 我是一名探鬼主播学辱,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼环形!你這毒婦竟也來了策泣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤抬吟,失蹤者是張志新(化名)和其女友劉穎萨咕,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體火本,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡危队,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了钙畔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茫陆。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖刃鳄,靈堂內(nèi)的尸體忽然破棺而出盅弛,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布挪鹏,位于F島的核電站见秽,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏讨盒。R本人自食惡果不足惜解取,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望返顺。 院中可真熱鬧禀苦,春花似錦、人聲如沸遂鹊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秉扑。三九已至慧邮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間舟陆,已是汗流浹背误澳。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留秦躯,地道東北人忆谓。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像踱承,于是被迫代替她去往敵國和親倡缠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,336評論 0 10
  • 不管你在中國的哪一個城市勾扭,只要你還準(zhǔn)備結(jié)婚毡琉,還有個工作,而且父母安在妙色,那你或許想過這件事情:在這個城市里,我是不是...
    旗襄閱讀 205評論 0 0
  • 大二剛要結(jié)尾慧耍,就要面臨人生的選擇身辨。到底是選擇考研還是直接工作。我也只是二十歲而已芍碧,就要自己去做這樣的選擇煌珊,真的好...
    小煥仔閱讀 506評論 12 1
  • 等待一個住在心里的人,當(dāng)耐心疏漏在消逝的時間之后泌豆,不安的黑精靈定庵,在每一條神經(jīng)上跳躍,它可知道它的快樂建立在承受...
    21歲的大女孩閱讀 164評論 0 0
  • 還是沒能憋住猪落,在房東的一句句扎心的話語下,今天終于還是哭了出來畴博。 搬家了笨忌,跟著房東一起搬的。 什么都變遭了俱病,本以為...
    小佛系的彩老師閱讀 235評論 2 0