百练1511:Invitation Cards题解
来源:互联网 发布:jquery.cookie.min.js 编辑:程序博客网 时间:2024/05/17 06:11
1511:Invitation Cards
题目链接:点击打开链接
代码:
#include<cstdio>#include<queue>#include<cstring>#include<vector>using namespace std;typedef long long ll;const int N = 1000005;typedef pair<int, int > PAIR;struct Edge{ int to; int price; int next;};Edge G[N],rG[N];int head1[N],head2[N];ll dis[N];void spfa(){ memset(dis,0x3f,sizeof(dis)); priority_queue<PAIR, vector<PAIR>, greater<PAIR> > que; que.push(PAIR(0,1)); dis[1] = 0; while(!que.empty()){ int u = que.top().second; que.pop(); for(int i = head1[u];i; i = G[i].next){ Edge e = G[i]; if(dis[e.to] > dis[u] + e.price){ dis[e.to] = dis[u] + e.price; que.emplace(dis[e.to],e.to); } } }}void spfa2(){ memset(dis,0x3f,sizeof(dis)); priority_queue<PAIR, vector<PAIR>, greater<PAIR> > que; que.push(PAIR(0,1)); dis[1] = 0; while(!que.empty()){ int u = que.top().second; que.pop(); for(int i = head2[u];i; i = rG[i].next){ Edge e = rG[i]; if(dis[e.to] > dis[u] + e.price){ dis[e.to] = dis[u] + e.price; que.emplace(dis[e.to],e.to); } } }}int main(){ int n,p,q; scanf("%d",&n); while(n--){ memset(head1,0,sizeof(head1)); memset(head2,0,sizeof(head2)); scanf("%d%d",&p,&q); for(int i = 1; i <= q; ++i){ int from,to,price; scanf("%d%d%d",&from,&to,&price); Edge e = {to,price,0}; G[i] = e; G[i].next = head1[from]; head1[from] = i; e.to = from; rG[i] = e; rG[i].next = head2[to]; head2[to] = i; } ll ans = 0; spfa(); for(int i = 2; i <= p; ++i) ans += dis[i]; spfa2(); for(int i = 2; i <= p; ++i) ans += dis[i]; printf("%lld\n", ans); } return 0;}
阅读全文
0 0
- 百练1511:Invitation Cards题解
- POJ 1511 Invitation Cards SPFA算法题解
- POJ 1511 Invitation Cards
- poj 1511 Invitation Cards
- 1511--Invitation Cards--Spfa
- poj 1511 Invitation Cards
- POJ 1511 Invitation Cards
- poj 1511 Invitation Cards
- POJ-1511-Invitation Cards
- POJ 1511 Invitation Cards
- poj 1511 Invitation Cards
- poj 1511 Invitation Cards
- poj 1511 Invitation Cards
- POJ 1511 Invitation Cards
- POJ 1511 Invitation Cards
- POJ-1511-Invitation Cards
- POJ 1511 Invitation Cards
- POJ 1511 Invitation Cards
- 运行wampserver遇到的vcruntime.dll丢失问题(初搞php的朋朋们来这看啊)
- Linux下查看进程打开的文件句柄数
- maven pom.xml配置详解
- PHP Cookie管理
- 浅谈数据库中的存储过程
- 百练1511:Invitation Cards题解
- 让apache服务器可以保证http和https同时访问(解决https可以访问而http不能访问)
- Gym
- OAuth了解
- 设备软件可靠性测试
- HDU 2066 一个人的旅行
- Bootstrap复选框、EasyUI datagrid复选框
- for循环嵌套的几种有意思的图形
- 设计模式5-建造者模式