poj 1511
来源:互联网 发布:2017网络热点 编辑:程序博客网 时间:2024/06/03 15:57
点击打开链接//dijkstra,有向图,逆邻接表//vector容器还是挺费时间的#include <iostream>#include<cstdio>#include<vector>#include<cstring>#include<queue>using namespace std;typedef long long LL;typedef pair<LL,LL>PII;const int maxn=1000010;const LL INF=1e10+10;struct Edge{int to;LL weight;Edge(int to,LL weight):to(to),weight(weight) {}};vector<Edge>edges[maxn];int u[maxn],pos[maxn]; //反向建图LL cost[maxn];LL d[maxn];bool done[maxn];int v,e;void dijkstra(){ memset(done,false,sizeof(done)); for(int i=1;i<=v;i++) d[i]=(i==1?0:INF); priority_queue<PII,vector<PII>,greater<PII> >pq; pq.push(PII(d[1],1)); while(!pq.empty()) { PII k=pq.top(); pq.pop(); int v1=k.second; if(done[v1]) continue; done[v1]=true; for(int i=0;i<edges[v1].size();i++) if(d[edges[v1][i].to]>d[v1]+edges[v1][i].weight) { d[edges[v1][i].to]=d[v1]+edges[v1][i].weight; pq.push(PII(d[edges[v1][i].to],edges[v1][i].to)); } }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&v,&e); for(int i=0;i<e;i++) { scanf("%d%d%lld",&u[i],&pos[i],&cost[i]); edges[u[i]].push_back(Edge(pos[i],cost[i])); } dijkstra(); LL sum=0; for(int i=1;i<=v;i++) { sum+=d[i]; edges[i].clear(); } //反向建图 for(int i=0;i<e;i++) edges[pos[i]].push_back(Edge(u[i],cost[i])); dijkstra(); for(int i=1;i<=v;i++) { sum+=d[i]; edges[i].clear(); } printf("%lld\n",sum); } return 0;}
阅读全文
0 0
- poj 1511
- POJ 1511
- poj 1511
- POJ 1511
- POJ 1511
- POJ 1511
- poj 1511
- POJ 1511
- poj---1511
- POJ 1511
- poj 1511
- POJ 1511
- poj 1511
- POJ 1511 Invitation Cards
- poj 1511 Invitation Cards
- poj 1511 Invitation Cards
- POJ 1511 Invitation Cards
- poj 1511 spfa
- JavaWeb例程二
- git切换分支(远程)
- numpy.eye() 生成对角矩阵
- 第二章 Java程序设计环境
- 锋利的SQL:从分组中取前几行数据
- poj 1511
- 2017-12-14战斗有限状态机FSM -第四天
- html自学记录3
- input[type="range"]是html5中的input标签新属性,样子如下:
- TWaver的Network组件相关方法
- noip2017普及组t2 图书管理员
- dumpsys命令的用法
- 一些数据API接口
- SQL UPDATE