Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 23357 | Accepted: 7675 |
Description
The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
Input
Output
Sample Input
2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
Sample Output
46
210
题意:求1号结点到其余各结点路径之和与其余各结点到1号结点之和的和。
思路:求其余各结点到1号结点的路径时可将有向边反向,转化为求1号结点到其余各结点的路径之和。注意:该题目的数据量较大,用动态邻接表存储会RE。
对比了一下 dijkstra 与 spfa算法。
/*
dijkstra 1511 Accepted 39744K 1907MS G++
*/
#include"cstdio"
#include"queue"
#include"vector"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
typedef long long LL;
typedef pair<int,int> P;
struct Edge{
int to,cost,next;
}es[][MAXN];
int V,E;
int head[][MAXN];
LL d[MAXN];
void add_edge(int u,int v,int cost,int type)
{
es[type][E].to=v;
es[type][E].cost=cost;
es[type][E].next=head[type][u];
head[type][u]=E;
} LL dijkstra(int s,int type)
{
for(int i=;i<=V;i++) d[i]=INF; priority_queue<P,vector<P>,greater<P> > que;
d[s]=,que.push(P(,s)); while(!que.empty())
{
P p=que.top();que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=head[type][v];i!=-;i=es[type][i].next)
{
Edge e=es[type][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
LL ans=;
for(int i=;i<=V;i++)
ans+=d[i];
return ans;
} int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int P,Q;
scanf("%d%d",&P,&Q);
V=P,E=;
for(int i=;i<=V;i++) head[][i]=head[][i]=-;
for(int i=;i<Q;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co,);
add_edge(v,u,co,);
E++;
} LL res=;
res+=dijkstra(,);
res+=dijkstra(,);
printf("%I64d\n",res); }
return ;
}
/*
spfa 1511 Accepted 43676K 1875MS G++
*/
#include"cstdio"
#include"queue"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
typedef long long LL;
struct Edge{
int to,cost,next;
}es[][MAXN];
int head[][MAXN];
int V,E;
LL d[MAXN];
int vis[MAXN];
LL spfa(int s,int type)
{
for(int i=;i<=V;i++)
{
d[i]=INF;
vis[i]=;
}
queue<int> que;
vis[s]=,d[s]=,que.push(s); while(!que.empty())
{
int v=que.front();que.pop();
vis[v]=;
for(int i=head[type][v];i!=-;i=es[type][i].next)
{
Edge e=es[type][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
if(!vis[e.to])
{
que.push(e.to);
vis[e.to]=;
}
}
}
}
LL ans=;
for(int i=;i<=V;i++)
ans+=d[i];
return ans;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int P,Q;
scanf("%d%d",&P,&Q);
V=P,E=;
for(int i=;i<=V;i++) head[][i]=head[][i]=-;
for(int i=;i<Q;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
es[][E].to=v,es[][E].cost=co,es[][E].next=head[][u],head[][u]=E;
es[][E].to=u,es[][E].cost=co,es[][E].next=head[][v],head[][v]=E;
E++;
} LL res=;
res+=spfa(,);
res+=spfa(,);
printf("%I64d\n",res); }
return ;
}
堆优化dijkstra 算法的复杂度为 |E|*log(|V|) ,优势在于处理稀疏图。