您的位置:首页 > 其它

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]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: