HDU 3790 最短路径问题 双重权值
2017-07-18 22:44
375 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3790
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
const int INF = 1<<30;
int d[maxn],vis[maxn],c[maxn];
struct node
{
int d;//距离
int x;//点的编号
bool operator < (const node &a)const
{
if(this->d!=a.d)
return this->d>a.d;
}
};
struct edge
{
int from,to;
int w,h;
};
priority_queue<node> q;
vector<edge> e;
vector<int> g[maxn];
void addedge(int from,int to,int w,int h)
{
edge t={from,to,w,h};
e.push_back(t);
edge t2={to,from,w,h};
e.push_back(t2);
int m=e.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
void dijstra(int s,int n)
{
while(!q.empty())q.pop();
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)d[i]=INF,c[i]=INF;
d[s]=0,c[s]=0;
node a={d[s],s};
q.push(a);
while(!q.empty())
{
node temp=q.top();q.pop();
int u=temp.x;
if(vis[u])continue;
vis[u]=1;
for(int i=0;i<g[u].size();i++)
{
int v=e[g[u][i]].to;
// if(vis[v])continue;
int w=e[g[u][i]].w;
int H=e[g[u][i]].h;//最短距离最先考虑,相等时在按花费考虑
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
c[v]=c[u]+H;
q.push((node){d[v],v});
}
else if(d[v]==d[u]+w&&c[v]>c[u]+H)
c[v]=c[u]+H;
}
}
}
int main()
{
int n,m;
// freopen("in.txt","r",stdin);
// freopen("out1.txt","w",stdout);
while(~scanf("%d%d",&n,&m))
{
if(m+n==0)break;
e.clear();
for(int i=1;i<=n;i++)g[i].clear();
for(int i=0;i<m;i++)
{
int u,v,w,h;
scanf("%d%d%d%d",&u,&v,&w,&h);
addedge(u,v,w,h);
}
int s,t;
scanf("%d%d",&s,&t);
dijstra(s,n);
printf("%d %d\n",d[t],c[t]);
}
}
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
const int INF = 1<<30;
int d[maxn],vis[maxn],c[maxn];
struct node
{
int d;//距离
int x;//点的编号
bool operator < (const node &a)const
{
if(this->d!=a.d)
return this->d>a.d;
}
};
struct edge
{
int from,to;
int w,h;
};
priority_queue<node> q;
vector<edge> e;
vector<int> g[maxn];
void addedge(int from,int to,int w,int h)
{
edge t={from,to,w,h};
e.push_back(t);
edge t2={to,from,w,h};
e.push_back(t2);
int m=e.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
void dijstra(int s,int n)
{
while(!q.empty())q.pop();
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)d[i]=INF,c[i]=INF;
d[s]=0,c[s]=0;
node a={d[s],s};
q.push(a);
while(!q.empty())
{
node temp=q.top();q.pop();
int u=temp.x;
if(vis[u])continue;
vis[u]=1;
for(int i=0;i<g[u].size();i++)
{
int v=e[g[u][i]].to;
// if(vis[v])continue;
int w=e[g[u][i]].w;
int H=e[g[u][i]].h;//最短距离最先考虑,相等时在按花费考虑
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
c[v]=c[u]+H;
q.push((node){d[v],v});
}
else if(d[v]==d[u]+w&&c[v]>c[u]+H)
c[v]=c[u]+H;
}
}
}
int main()
{
int n,m;
// freopen("in.txt","r",stdin);
// freopen("out1.txt","w",stdout);
while(~scanf("%d%d",&n,&m))
{
if(m+n==0)break;
e.clear();
for(int i=1;i<=n;i++)g[i].clear();
for(int i=0;i<m;i++)
{
int u,v,w,h;
scanf("%d%d%d%d",&u,&v,&w,&h);
addedge(u,v,w,h);
}
int s,t;
scanf("%d%d",&s,&t);
dijstra(s,n);
printf("%d %d\n",d[t],c[t]);
}
}
相关文章推荐
- HDU 3790 最短路径问题 【双重权值问题】
- HDU 3790 最短路径问题 (双重权值) dp
- hdu 3790 最短路径问题(双重权值,dijkstra算法)
- HDU-3790 最短路径问题(双重权值)
- HDU 3790 简单最短路径问题(dijkstra+双重权值)
- HDU 3790 最短路径问题 (双重权值)
- HDU 3790 ——最短路径问题 以边表为数据结构的BF算法&双重权值
- hdu 3790 最短路径问题(双重权值,dijkstra算法)
- hdu 3790 最短路径问题 【双权值最短路(相同最小距离,求最小花费)】
- POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
- HDU-3790 最短路径问题(两个权值,Dijkstra,(含堆优化))
- HDU 3790 最短路径问题【最短路 dijkstra 双权值】
- HDU 3790 最短路径问题 (dijkstra+路长和权值)
- [HDU] 3790 最短路径问题-一条边有两种权值
- HDU 3790 最短路径问题 (dijkstra+路长和权值)
- HDU 3790 最短路径问题 (dijkstra+路长和权值)
- POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
- HDU 3790 最短路径问题 (dijkstra+路长和权值)
- HDU 3790 最短路径问题 (dijkstra+路长和权值)
- HDU 3790 最短路径问题 (dijkstra+路长和权值)