zoj 3946 Highway Project(最短路径)
2016-04-25 13:32
274 查看
题目链接
题意:最短路径,每个边上有2个权值,第二个权值是不能累加的
题意:最短路径,每个边上有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; }
相关文章推荐
- HDOJ4431Mahjong
- ubuntu 安装截图工具 Shutter,并设置快捷键 Ctrl+Alt+A
- 数据库索引以及优化
- uibutton
- 字符编码转换(utf8-unicode)
- View的事件体系之--View的事件分发机制
- Github的项目托管
- C# 使用xenocode混淆加密【转】
- standby报错:PING[ARC2]: Heartbeat failed to connect to standby 'orcl'. Error is 16009.
- linux下Bash编程循环语句特殊用法之编写脚本(十)
- 文章标题
- 【学习Android】Keyboard.class类源码注解
- python两种生成md5的方法
- 关于开发React Native的注意事项
- 数组与指针运算
- SSL构建单双向https认证
- C语言for循环
- x86虚拟化之迷
- 装箱子
- Visual Studio常用快捷键