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]);
}
}
}
题目大意:让你求解出最安全的路径。
题目思路:直接套用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]);
}
}
}
相关文章推荐
- HDU1596 find the safest road(最短路变形,Dijkstra)
- HDOJ 1596 find the safest road(最短路--dijkstra变形)
- HDU1596:find the safest road(Dijkstra)
- find the safest road---hdu1596(最短路模板求最大概率)
- (阶段三 dijkstra1.4)HDU 1596 find the safest road(最短路的变形题:求乘积,求最大值)
- HDU1596 find the safest road---(最短路径dijkstra,#变形#)
- HDU1596-find the safest road(Dijkstra小变换)
- hdu1596 find the safest road【最短路dijkstra&&SPFA】
- hdu1596 find the safest road(dijkstra求最短路)
- HDU 1596 find the safest road【dijkstra & floyed & SPFA 】
- HDU-1596 find the safest road(最短路[Dijkstra])
- 【最短路+dijkstra+floyd+spfa】1596 find the safest road
- HDU1596 find the safest road(最短路)
- hdu 1596 find the safest road (最短路径dijkstra SPFA Floyd)
- hdu 1596 find the safest road(Dijkstra)
- hdu1596 find the safest road 最短路也能求最大值
- HDU - 1596 find the safest road (dijkstra)
- HDU1596 find the safest road 解题报告--Dijkstra
- HDU 1596 find the safest road <SPFA算法的一个变形>
- HDU1596 find the safest road(floyd)