今天做天梯賽訓練題L2-01
L2-001 緊急救援 (25 分)
作爲一個城市的應急救援隊伍的負責人,你有一張特殊的全國地圖。在地圖上顯示有多個分散的城市和一些連接城市的快速道路。每個城市的救援隊數量和每一條連接兩個城市的快速道路長度都標在地圖上。當其他城市有緊急求助電話給你的時候,你的任務是帶領你的救援隊儘快趕往事發地,同時,一路上召集儘可能多的救援隊。
輸入格式:
輸入第一行給出4個正整數N、M、S、D,其中N(2≤N≤500)是城市的個數,順便假設城市的編號爲0 ~ (N−1);M是快速道路的條數;S是出發地的城市編號;D是目的地的城市編號。
第二行給出N個正整數,其中第i個數是第i個城市的救援隊的數目,數字間以空格分隔。隨後的M行中,每行給出一條快速道路的信息,分別是:城市1、城市2、快速道路的長度,中間用空格分開,數字均爲整數且不超過500。輸入保證救援可行且最優解唯一。
輸出格式:
第一行輸出最短路徑的條數和能夠召集的最多的救援隊數量。第二行輸出從S到D的路徑中經過的城市編號。數字間以空格分隔,輸出結尾不能有多餘空格。
輸入樣例:
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2
輸出樣例
這裏的數據量很小,自然用不着優先隊列,但是今天就是想練一練
於是寫了這麼一個玩意,只有18分。
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
struct edge
{
int to;
int cost;
};
typedef pair<int,int>P;
vector<edge> e[1005];
int ren[1005];
int ren2[1005];
int d[1005];
int pre[1005];
int next2[1005];
int n,m,s,dis;
int tiao[1005];
bool visit[1005];
bool operator<(P x,P y)
{
return x.first>y.first;
}
void dijkstra(int s)
{
memset(visit,false,sizeof(visit));
fill(d,d+n,2140000);
d[s]=0;tiao[s]=1;
priority_queue<P> que;
que.push(P(0,s));
while (!que.empty())
{
P p=que.top();que.pop();
int v=p.second;visit[v]=true;
if (d[v]<p.first)continue;
for (int i=0;i<e[v].size();i++)
{
edge temp=e[v][i];
if (d[temp.to]==d[v]+temp.cost)
{
tiao[temp.to]+=tiao[v];
if (ren2[temp.to]<ren2[v]+ren[temp.to])
{
que.push(P(d[temp.to],temp.to));
ren2[temp.to]=ren2[v]+ren[temp.to];
pre[temp.to]=v;
}
}
if (d[temp.to]>d[v]+temp.cost)
{
tiao[temp.to]=tiao[v];
d[temp.to]=d[v]+temp.cost;
que.push(P(d[temp.to],temp.to));
ren2[temp.to]=ren2[v]+ren[temp.to];
pre[temp.to]=v;
}
}
}
}
int main()
{
memset(tiao,0,sizeof(tiao));
scanf("%d%d%d%d",&n,&m,&s,&dis);
for (int i=0;i<n;i++)
{
scanf("%d",&ren[i]);
ren2[i]=ren[i];
}
for (int i=0;i<m;i++)
{
edge temp;int start,x1,x2;
scanf("%d%d%d",&start,&x1,&x2);
temp.to=x1;temp.cost=x2;
e[start].push_back(temp);
temp.to=start;temp.cost=x2;
e[x1].push_back(temp);
}
dijkstra(s);
//dfs(s,-1,0,0);
printf("%d %d\n",tiao[dis],ren2[dis]);
int ttt=dis;
int j=1;
next2[0]=ttt;
while (ttt!=s)
{
ttt=pre[ttt];
next2[j++]=ttt;
}
for (int i=j-1;i>0;i--)
printf("%d ",next2[i]);
printf("%d\n",dis);
}
後來老老實實改寫成普通的dijkstra,如下:
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
struct edge
{
int to;
int cost;
};
typedef pair<int,int>P;
vector<edge> e[1005];
int ren[1005];
int ren2[1005];
int d[1005];
int pre[1005];
int next2[1005];
int n,m,s,dis;
int tiao[1005];
int visit[1005];
bool operator<(P x,P y)
{
return x.first>y.first;
}
void dijkstra(int s)
{
fill (d,d+n,2140000000);
fill (visit,visit+n,0);
d[s]=0;tiao[s]=1;
while (true)
{
int v=-1;
for (int u=0;u<n;u++)
{
if (!visit[u]&&(v==-1||d[u]<d[v]))
v=u;
}
//printf("%d\n",v);
if (v==-1)break;
visit[v]=1;
for (int u=0;u<e[v].size();u++)
{
edge temp=e[v][u];
if (d[temp.to]==d[v]+temp.cost)
{
tiao[temp.to]+=tiao[v];
if (ren2[temp.to]<ren[temp.to]+ren2[v])
{
ren2[temp.to]=ren[temp.to]+ren2[v];
pre[temp.to]=v;
}
}
if (d[temp.to]>d[v]+temp.cost)
{
d[temp.to]=d[v]+temp.cost;
ren2[temp.to]=ren[temp.to]+ren2[v];
pre[temp.to]=v;
tiao[temp.to]=tiao[v];
}
}
}
}
int main()
{
memset(tiao,0,sizeof(tiao));
scanf("%d%d%d%d",&n,&m,&s,&dis);
for (int i=0;i<n;i++)
{
scanf("%d",&ren[i]);
ren2[i]=ren[i];
}
for (int i=0;i<m;i++)
{
edge temp;int start,x1,x2;
scanf("%d%d%d",&start,&x1,&x2);
temp.to=x1;temp.cost=x2;
e[start].push_back(temp);
temp.to=start;temp.cost=x2;
e[x1].push_back(temp);
}
dijkstra(s);
//dfs(s,-1,0,0);
printf("%d %d\n",tiao[dis],ren2[dis]);
int ttt=dis;
int j=1;
next2[0]=ttt;
while (ttt!=s)
{
ttt=pre[ttt];
next2[j++]=ttt;
}
for (int i=j-1;i>0;i--)
printf("%d ",next2[i]);
printf("%d\n",dis);
}
得了滿分
希望大神告訴我們爲什麼。我仔細想了想,應該是優先隊列會有重複的情況多次出現,之前已經確定的點,可能還是會進入隊列裏面。而在普通的dijkstra就不會。