hdu 1535 Invitation Cards
2014-04-25 21:23
253 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1535
这道题两遍spfa,第一遍sfpa之后,重新建图,所有的边逆向建边,再一次spfa就可以了。
View Code
这道题两遍spfa,第一遍sfpa之后,重新建图,所有的边逆向建边,再一次spfa就可以了。
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <algorithm> #define maxn 1000001 using namespace std; const int inf=1000000000; long long dis1[maxn]; long long dis2[maxn]; int e1,head[maxn],e2; bool vis[maxn]; int cnt[maxn]; int P,Q,s,e,n; long long w; struct node { int u,v,next; long long w; }p[maxn],p1[maxn]; void add(int u,int v,long long w) { p[e1].u=u; p[e1].v=v; p[e1].w=w; p[e1].next=head[u]; head[u]=e1++; } bool spfa(int src,long long dis[]) { queue<int>q; memset(cnt,0,sizeof(cnt)); memset(vis,false,sizeof(vis)); for(int i=1; i<=n; i++) dis[i]=inf; dis[src]=0; vis[src]=true; q.push(src); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=head[u]; i!=-1; i=p[i].next) { int vv=p[i].v; long long ww=p[i].w; if(dis[vv]>dis[u]+ww) { dis[vv]=dis[u]+ww; if((++cnt[vv])>n) return false; if(!vis[vv]) { q.push(vv); vis[vv]=true; } } } } return true; } int main() { int t; scanf("%d",&t); while(t--) { e1=0; memset(head,-1,sizeof(head)); scanf("%d%d",&P,&Q); for(int i=0; i<Q; i++) { cin>>p1[i].u>>p1[i].v>>p1[i].w; add(p1[i].u,p1[i].v,p1[i].w); } n=P; spfa(1,dis1); long long sum=0; e1=0; memset(head,-1,sizeof(head)); for(int i=0; i<Q; i++) { add(p1[i].v,p1[i].u,p1[i].w); } spfa(1,dis2); for(int i=2; i<=n; i++) { sum+=(dis1[i]+dis2[i]); } cout<<sum<<endl; } return 0; }
View Code
相关文章推荐
- HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)
- HDU 1535 Invitation Cards(SPFA,及其优化)
- HDU 1535 Invitation Cards(dijkstra)
- HDU-1535-Invitation Cards
- HDU - 1535 - Invitation Cards
- Invitation Cards HDU - 1535 基础最短路
- HDU - 1535 Invitation Cards (最短路dijkstra)
- hdu 1317+hdu 1535(SPFA)
- HDU 1535 Invitation Cards SPFA
- hdu 1535 Invitation Cards(SPFA)
- HDU 1535 Invitation Cards(最短路 spfa)
- HDU 1535 Invitation Cards
- HDU 1535 Invitation Cards 正反向建图
- HDU 1535 Invitation Cards
- HDU-1535-Invitation Cards(SPFA,邻接表)
- HDU 1535 Invitation Cards(BellmanFord)
- HDU 1535 Invitation Cards (最短路,附SLF优化SPFA)
- hdu 1535 Invitation Cards(邻接表spfa)
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
- hdu 1535 || poj 1511 Invitation Cards(heap + dijstra || spfa)