您的位置:首页 > 其它

zoj 3946 Highway Project(最短路径)

2016-04-25 13:32 274 查看
题目链接

题意:最短路径,每个边上有2个权值,第二个权值是不能累加的

#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define cl(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define gcd __gcd

const int maxn = 100005;
const LL inf  =1LL<<50;
const LL mod1 = 1000000007;
int n,m;
struct edge{
int to;
LL cost1,cost2;
};
vector<edge> es;
vector<int> G[maxn];
bool used[maxn];
LL d1[maxn],d2[maxn];

void init(){
es.clear();
for(int i=0;i<maxn;i++){
G[i].clear();used[i]=false;
d1[i]=d2[i]=inf;
}
}

void addEdge(int from,int to,int w1,int w2){
es.push_back(edge{to,w1,w2});
es.push_back(edge{from,w1,w2});
int x=es.size();
G[from].push_back(x-2);
G[to].push_back(x-1);
}
struct node{
int cur;
int cost1,cost2;
bool operator<(const node& rhs) const {
if(cost1>rhs.cost1)return 1;
else if(cost1==rhs.cost1&&cost2>rhs.cost2)return 1;
else return 0;
}
};

void dij(int s){
priority_queue<node> q;
q.push(node{s,0,0});
d1[s]=d2[s]=0;

while(!q.empty()){
node x=q.top();q.pop();
if(used[x.cur])continue;
used[x.cur]=true;
for(int i=0;i<G[x.cur].size();i++){
edge &e=es[G[x.cur][i]];
if(d1[e.to]>d1[x.cur]+e.cost1){
d1[e.to]=d1[x.cur]+e.cost1;
d2[e.to]=e.cost2;
q.push(node{e.to,d1[e.to],d2[e.to]});
}
else if(d1[e.to]==d1[x.cur]+e.cost1&&d2[e.to]>e.cost2){
d2[e.to]=e.cost2;
q.push(node{e.to,d1[e.to],d2[e.to]});
}
}
}
LL sum=0,res=0;
for(int i=0;i<n;i++){
sum+=d1[i];res+=d2[i];
}
printf("%lld %lld\n",sum,res);
}

int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++){
int x,y,c,d;
scanf("%d%d%d%d",&x,&y,&c,&d);
addEdge(x,y,c,d);
}
dij(0);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: