HDU 1535 Invitation Cards SPFA
2017-09-05 22:06
260 查看
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1535题意
给定单向图,从1点走到其他任意一点,再返回n,直到所有点走完,求最短路。思路
图是单向的,从1点跑最短路可以得到1到其他点的最短距离。反向建图从1点跑最短路可以得到其他点到1点的最短距离,边要先保存起来,正向跑完后销毁,建反向再跑一次。同时建正向和反向超内存。。#include<cstdio> #include<queue> #include<iostream> #include<vector> #include<map> #include<cstring> #include<string> #include<set> #include<stack> #include<algorithm> #define cle(a) memset(a,0,sizeof(a)) #define inf(a) memset(a,0x3f,sizeof(a)) #define ll long long #define Rep(i,a,n) for(int i=a;i<=n;i++) using namespace std; #define INF2 9223372036854775807ll const int INF = ( 2e9 ) + 2; const ll maxn = 1000010; struct edge { int v,w,next; }e[maxn]; struct Edge { int u,v,w; }; vector<Edge> E; int vis[maxn],d[maxn],head[maxn]; int tot; int SPFA(int s,int n) { queue<int> q; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++)d[i]=INF; d[s]=0; q.push(s); vis[s]=1; while(!q.empty()) { int u=q.front();q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; int w=e[i].w; if(d[v]>d[u]+w) { d[v]=d[u]+w; if(!vis[v]) { vis[v]=1; q.push(v); } } } } int ret=0; for(int i=1;i<=n;i++)ret+=d[i]; return ret; } int main() { int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); E.clear(); tot=0; memset(head,-1,sizeof(head)); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); E.push_back(Edge{u,v,w}); } for(int i=0,L=E.size();i<L;i++) { int u=E[i].u,v=E[i].v,w=E[i].w; e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++; } int a1=SPFA(1,n); tot=0; memset(head,-1,sizeof(head)); for(int i=0,L=E.size();i<L;i++) { int u=E[i].u,v=E[i].v,w=E[i].w; e[tot].v=u; e[tot].w=w; e[tot].next=head[v]; head[v]=tot++; } int a2=SPFA(1,n); printf("%d\n",a1+a2); } }
相关文章推荐
- HDU 1535 && POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)
- (spfa专题 1.1)hdu 1535 Invitation Cards(求从1到2~n的点后,再从2~n返回1点的最小距离)
- HDU 1535 Invitation Cards 有向图的来回最短路(邻接表反向建图)+spfa
- hdu 1535 Invitation Cards(SPFA)
- hdu 1535 Invitation Cards(spfa)
- HDU 1535 SPFA 前向星存图优化
- HDU 1535 Invitation Cards(最短路 spfa)
- hdu 1535 Invitation Cards【SPFA】
- hdu 1535 Invitation Cards(SPFA)
- HDU 1535 Invitation Cards【SPFA最短路】【正反向建边求单源最短路之和】
- hdu 1535 || poj 1511 Invitation Cards(heap + dijstra || spfa)
- hdu1535—Invitation Cards(spfa)
- HDU 1535 Invitation Cards(多源点到单源点,dijkstra/spfa)
- hdu 1535 Invitation Cards(邻接表spfa)
- HDU-1535-Invitation Cards(SPFA,邻接表)
- HDU 1535 Invitation Cards(SPFA,及其优化)
- HDU 1535 Invitation Cards (spfa, 链式前向星,逆向建图)
- HDU 1535 Invitation Cards(有向图单源最短路径+SPFA)
- [HDU 1535]Invitation Cards[SPFA反向思维]
- 【SPFA邻接表】HDU 1535 Invitation Cards