poj 1511 Invitation Cards(2遍spfa)
来源:互联网 发布:soc软件品牌 编辑:程序博客网 时间:2024/06/05 05:44
题目地址
题目大意:n个站,告诉站之间的权值,求从1到所有点并返回1的最小权值和
解题思路:对1进行单源spfa,返回的时候因为spfa只能单源,所以将原图转置,再对1进行一次spfa,图的转置需要特殊处理,我采用的是邻接表存储图,相当于建了2个图
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <algorithm>#include <queue>#include <stack>#include <vector>using namespace std;const int maxn = 1000000+10;const int INF = 0xfffffff;int dis[maxn],head[maxn],vis[maxn],cnt,n,m;int u[maxn],v[maxn],w[maxn];struct Edge{ int to,next,w;}e[maxn];void addEdge(int u,int v,int w){ e[cnt].to = v; e[cnt].w = w; e[cnt].next = head[u]; head[u] = cnt++;}void spfa(){ queue<int> q; while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) dis[i] = INF; q.push(1); vis[1] = 1; dis[1] = 0; while(!q.empty()) { int v = q.front(); q.pop(); vis[v] = 0; for(int i = head[v]; i != -1; i = e[i].next) { if(dis[e[i].to] > dis[v]+e[i].w) { dis[e[i].to] = dis[v]+e[i].w; if(!vis[e[i].to]) { vis[e[i].to] = 1; q.push(e[i].to); } } } }}void graph1(){ cnt = 1; memset(head,-1,sizeof(head)); for(int i = 1; i <= m; i++) addEdge(u[i],v[i],w[i]);}void graph2(){ cnt = 1; memset(head,-1,sizeof(head)); for(int i = 1; i <= m; i++) addEdge(v[i],u[i],w[i]);}int main(){ int t,sum; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i = 1; i <= m; i++) { scanf("%d%d%d",&u[i],&v[i],&w[i]); } sum = 0; graph1(); spfa(); for(int i = 2; i <= n; i++) sum += dis[i]; graph2(); spfa(); for(int i = 2; i <= n; i++) sum += dis[i]; printf("%d\n",sum); }return 0;}
0 0
- poj 1511 Invitation Cards(2遍spfa)
- poj 1511 Invitation Cards (spfa)
- POJ 1511 Invitation Cards (spfa)
- POJ 1511 Invitation Cards(SPFA)
- POJ 1511 Invitation Cards //spfa
- poj 1511 Invitation Cards--SPFA
- POJ 1511 Invitation Cards (Spfa)
- poj 1511 Invitation Cards(spfa)
- POJ 1511 Invitation Cards (SPFA)
- poj 1511 Invitation Cards(spfa)
- Poj 1511 Invitation Cards -- spfa
- POJ 1511 Invitation Cards(SPFA)
- POJ 1511 Invitation Cards(SPFA)
- Invitation Cards - POJ 1511 spfa
- Poj 1511 Invitation Cards(spfa)
- poj Invitation Cards(spfa)
- POJ 1511 Invitation Cards (spfa||堆优化)
- POJ 1511 Invitation Cards 求来回最短路(spfa)
- Linux Socket编程
- gdb调试命令
- 树莓派Raspbian系统定制 - LXDE桌面系统定制 - 基本概念
- 解决svn执行cleanup出现failed–previous operation has not finished; run cleanup if it was interrupted 的错误
- Android地图添加标记和文字【代码片段】
- poj 1511 Invitation Cards(2遍spfa)
- 安卓开发第十二天
- UVA 10194 Football (aka Soccer)
- AIX 未开启AIO引起SQLPLUS登陆报错exec(): 0509-036
- Mysql基本检索数据方法大全
- Openstack magnum 简介
- ubuntu 14.04 降内核版本 -- 原因是 perf 版本与内核版本不配套
- mysql workbench 报 tables could not be fetched.
- moc