SPFA + vector邻接表 居然TLE。 Time Limit: 8000MS
然后又是尝试一下 堆优化的Dijkstra + vector邻接表 果然还是TLE。
放弃vector,转为静态。
SPFA 1891MS
Dijkstra_heap 1938MS
突然发现以前做过这个题。搜一下自己的Blog SPFA + vector 7063MS 危险的过了……想不通了。。。
Dijkstra_heap
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<bitset> #include<vector> #include<cmath> #define INF 0x7fffffff #define eps 1e-8 #define LL long long #define PI 3.141592654 #define CLR(a,b) memset(a,b,sizeof(a)) #define FOR(i,a,b) for(int i=a;i<b;i++) #define FOR_(i,a,b) for(int i=a;i>=b;i--) #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define mp make_pair #define ft first #define sd second #define sf scanf #define pf printf #define sz(v) ((int)(v).size()) #define all(v) (v).begin(),(v).end() #define acfun std::ios::sync_with_stdio(false) #define SIZE 1000000 +1 using namespace std; int head1[SIZE]; int head2[SIZE]; int n,m; int cot1,cot2; struct lx { int v,l,next; void init(int vv,int ll,int nn) { v=vv,l=ll,next=nn; } }; lx edge1[SIZE]; lx edge2[SIZE]; void add(int u,int v,int l) { edge1[cot1].init(v,l,head1[u]); head1[u]=cot1++; edge2[cot2].init(u,l,head2[v]); head2[v]=cot2++; } void show(int *head,lx *edge) { FOR(i,1,n+1) { int tmp=head[i]; while(tmp!=-1) { pf("%d->%d=%d ",i,edge[tmp].v,edge[tmp].l); tmp=edge[tmp].next; } pf("\n"); } } struct node { int s,l; node(int ss,int ll) { s=ss,l=ll; } friend bool operator <(node a,node b) { if(a.l==b.l)return a.s<b.s; return a.l<b.l; } }; int dis[SIZE]; LL Dijkstra(int *head,lx *edge) { priority_queue<node>q; FOR(i,0,n+1) dis[i]=INF; dis[1]=0; q.push(node(1,dis[1])); while(!q.empty()) { node tmp=q.top(); q.pop(); int u=tmp.s; dis[u]=tmp.l; int next=head[u]; while(next!=-1) { int v=edge[next].v; int l=edge[next].l; if(dis[v]>dis[u]+l) { dis[v]=dis[u]+l; q.push(node(v,dis[v])); } next=edge[next].next; } } LL ans=0; FOR(i,1,n+1) ans+=dis[i]; return ans; } int main() { int t; sf("%d",&t); while(t--) { sf("%d%d",&n,&m); CLR(head1,-1); CLR(head2,-1); cot1=cot2=0; while(m--) { int u,v,l; sf("%d%d%d",&u,&v,&l); add(u,v,l); } //show(head1,edge1); LL ans=0; ans+=Dijkstra(head1,edge1); ans+=Dijkstra(head2,edge2); pf("%lld\n",ans); } }
SPFA
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<bitset>
#include<vector>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR_(i,a,b) for(int i=a;i>=b;i--)
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define acfun std::ios::sync_with_stdio(false)
#define SIZE 1000000 +1
using namespace std;
int n,m;
int head1[SIZE],head2[SIZE];
int dis[SIZE];
bool vis[SIZE];
struct lx
{
int v,l,next;
void init(int vv,int ll,int nn)
{
v=vv,l=ll,next=nn;
}
};
lx edge1[SIZE];
lx edge2[SIZE];
int cot1,cot2;
void add(int u,int v,int l)
{
edge1[cot1].init(v,l,head1[u]);
head1[u]=cot1++;
edge2[cot2].init(u,l,head2[v]);
head2[v]=cot2++;
}
void show(int *head,lx *edge)
{
FOR(i,1,n+1)
{
int tmp=head[i];
while(tmp!=-1)
{
pf("%d->%d=%d ",i,edge[tmp].v,edge[tmp].l);
tmp=edge[tmp].next;
}
pf("\n");
}
}
LL SPFA(int *head,lx *edge)
{
FOR(i,0,n+1)
dis[i]=INF;
queue<int>q;
CLR(vis,0);
q.push(1);
vis[1]=1;
dis[1]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
int tmp=head[u];
while(tmp!=-1)
{
int v=edge[tmp].v;
int l=edge[tmp].l;
if(dis[v]>dis[u]+l)
{
dis[v]=dis[u]+l;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
tmp=edge[tmp].next;
}
}
LL ans=0;
FOR(i,1,n+1)
ans+=dis[i];
return ans;
}
int main()
{
int t;
sf("%d",&t);
while(t--)
{
sf("%d%d",&n,&m);
CLR(head1,-1);
CLR(head2,-1);
cot1=cot2=0;
while(m--)
{
int u,v,l;
sf("%d%d%d",&u,&v,&l);
add(u,v,l);
}
//show(head1,edge1);
LL ans=0;
ans+=SPFA(head1,edge1);
ans+=SPFA(head2,edge2);
pf("%lld\n",ans);
}
}
原文地址:http://blog.csdn.net/dongshimou/article/details/41479509