您的位置:首页 > 其它

最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)

2017-04-25 16:54 639 查看
题目链接:http://poj.org/problem?id=1511

Invitation Cards

Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 27231 Accepted: 9034
Description

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information
and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special
course was taken where students learned how to influence people and what is the difference between influencing and robbery. 

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

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number
of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which
is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.
Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.
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

题意:现在有n个点,m条单向边;现在求每个点走到点1,在返回到原来的位置的最短路距离之和;(意思就是每个点都要去一趟点1,在返回,求所有点的所走距离之和的最小值)
思路:从点1到每个点的距离最小值能用spfa求出来;那么反过来每个点到点1的距离也能求出来;

我自己写的spfa方法:AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const LL inf = 0x3f;
const int maxx = 1e6 +10;
int t,n,m;
struct node
{
int x,y,next,nexx;
LL num;
}point[maxx];
int head[maxx];int head2[maxx];//head[i]表示i能到的地方:与next对应;head2[i]表示能到i的地方:与nexx对应;
LL dis[maxx];
int vis[maxx];
LL spfa1()
{
memset(vis,0,sizeof(vis));
memset(dis,inf,sizeof(dis));//dis[i]表示1到i的最小距离;
queue<int>q;
q.push(1);
vis[1]=1;
dis[1]=0;
while(!q.empty())
{
int f=q.front();
q.pop();
vis[f]=0;
for(int i=head[f];i;i=point[i].next)
{
if(dis[point[i].x]+point[i].num<dis[point[i].y])
{
dis[point[i].y]=dis[point[i].x]+point[i].num;
if(!vis[point[i].y])
{
vis[point[i].y]=1;
q.push(point[i].y);
}
}
}
}
LL ans=0;
for(int i=1;i<=n;i++)
ans+=dis[i];
return ans;
}
LL spfa2()
{
memset(vis,0,sizeof(vis));
memset(dis,inf,sizeof(dis));//dis[i]表示i到1的最小距离
queue<int>q;
q.push(1);
vis[1]=1;
dis[1]=0;
while(!q.empty())
{
int f=q.front();
q.pop();
vis[f]=0;
for(int i=head2[f];i;i=point[i].nexx)
{
if(dis[point[i].y]+point[i].num<dis[point[i].x])
{
dis[point[i].x]=dis[point[i].y]+point[i].num;
if(!vis[point[i].x])
{
vis[point[i].x]=1;
q.push(point[i].x);
}
}
}
}
LL ans=0;
for(int i=1;i<=n;i++)
ans+=dis[i];
return ans;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(head,0,sizeof(head));
memset(head2,0,sizeof(head2));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld",&point[i].x,&point[i].y,&point[i].num);
point[i].next=head[point[i].x];//spfa1的
head[point[i].x]=i;
point[i].nexx=head2[point[i].y];//spfa2的,邻接表用head数组表示
head2[point[i].y]=i;
}
printf("%lld\n",spfa1()+spfa2());
}
}
kuangbin大神的代码:(单源最短路,优先队列优化的Dijkstra)
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <
4000
algorithm>
#include <queue>
#include <vector>
using namespace std;
/*
* 使用优先队列优化Dijkstra算法
* 复杂度O(ElogE)
* 注意对vector<Edge>E[MAXN]进行初始化后加边
*/
const int INF=0x3f3f3f3f;
const int MAXN=1000010;
struct qnode
{
int v;
int c;
qnode(int _v=0,int _c=0):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int n,int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=0;
que.push(qnode(start,0));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=0;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
}
int A[MAXN],B[MAXN],C[MAXN];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,m;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d%d%d",&A[i],&B[i],&C[i]);
for(int i=1;i<=n;i++)E[i].clear();
for(int i=0;i<m;i++)addedge(A[i],B[i],C[i]);
Dijkstra(n,1);
long long ans=0;
for(int i=1;i<=n;i++)ans+=dist[i];
for(int i=1;i<=n;i++)E[i].clear();
for(int i=0;i<m;i++)addedge(B[i],A[i],C[i]);
Dijkstra(n,1);
for(int i=1;i<=n;i++)ans+=dist[i];
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  最短路 dijkstra poj spfa