您的位置:首页 > Web前端

hdu1596 find the safest road(DijKstra模板简单变形)

2017-07-24 22:30 411 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1596

题目大意:让你求解出最安全的路径。

题目思路:直接套用Dijkstra模板,只需要对一些地方进行修改,优先队列从大到小排列,inf改为1,加法改为乘积,等等等。

学到的东西:double scanf用 %lf 输出用%f 同float

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxv=1e3+10;
const double inf=0;
struct edge
{
int to;
double cost;
};
typedef pair<double,int> P;
int V,E;
vector<edge>G[maxv];
double d[maxv];

void dijkstra(int s)
{
priority_queue<P,vector<P> >que;
fill(d,d+V+1,inf);
d[s]=1;
que.push(P(0,s));

while(!que.empty()){
P p = que.top();que.pop();
int v = p.second;
if(d[v]<p.first) continue;
for(int i=0;i<G[v].size();i++){
edge e=G[v][i];
if(d[e.to]<d[v]*e.cost){
d[e.to]=d[v]*e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}

int main()
{
while(~scanf("%d",&V)){
for(int i=1;i<=V;i++) G[i].clear();
for(int i=1;i<=V;i++)
for(int j=1;j<=V;j++){
double x;scanf("%lf",&x);
edge e;e.to=j;e.cost=x;
G[i].push_back(e);
}
int t;cin>>t;
while(t--){
int u,v;scanf("%d%d",&u,&v);
dijkstra(u);
if(d[v]==0) printf("What a pity!\n");
else printf("%.3f\n",d[v]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: