POJ - 1511 Invitation Cards (SPFA最短路)
2017-08-15 19:21
537 查看
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.
题意:求点1到各点的距离和以及各点到点1的距离和的总和
思路:这道题看似简单,其实这个数据。。。貌似有点大,一开始用spfa的vector的邻接表做,8000ms无限T,有点懵逼,然后看题解,原来只能用静态邻接表做。。。最后2500多ms,快了不少。
代码:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const int maxn = 1e7 + 5;
const int mod = 1e8 + 7;
int N,n,m;
int head[2][maxn];
int cnt=0;
int tmp;
bool vis[maxn];
LL d[maxn];
struct Edge{
int u,v,cost;
int next;
}edge[2][maxn];
void add(int u,int v,int c){
edge[tmp][cnt].u=u;
edge[tmp][cnt].v=v;
edge[tmp][cnt].cost=c;
edge[tmp][cnt].next=head[tmp][u];
head[tmp][u]=cnt++;
}
void spfa(int s){
queue<int>q;
memset(vis,0,sizeof(vis));
memset(d,INF,sizeof(d));
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[tmp][u];~i;i=edge[tmp][i].next){
Edge e=edge[tmp][i];
if (d[e.v]>e.cost+d[u]){
d[e.v]=e.cost+d[u];
if (!vis[e.v]){
q.push(e.v);
vis[e.v]=1;
}
}
}
}
}
int main() {
//freopen ("in.txt", "r", stdin);
scanf ("%d",&N);
while (N--){
scanf ("%d%d",&n,&m);
for (int i=1;i<=n;i++) head[0][i]=head[1][i]=-1;
int u,v,c;
while (m--){
scanf ("%d%d%d",&u,&v,&c);
tmp=0;
add(u,v,c);
tmp=1;
add(v,u,c);
}
LL ans=0;
tmp=0;
spfa(1);
for (int i=1;i<=n;i++) ans+=d[i];
tmp=1;
spfa(1);
for (int i=1;i<=n;i++) ans+=d[i];
printf ("%lld\n",ans);
}
return 0;
}
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.
题意:求点1到各点的距离和以及各点到点1的距离和的总和
思路:这道题看似简单,其实这个数据。。。貌似有点大,一开始用spfa的vector的邻接表做,8000ms无限T,有点懵逼,然后看题解,原来只能用静态邻接表做。。。最后2500多ms,快了不少。
代码:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const int maxn = 1e7 + 5;
const int mod = 1e8 + 7;
int N,n,m;
int head[2][maxn];
int cnt=0;
int tmp;
bool vis[maxn];
LL d[maxn];
struct Edge{
int u,v,cost;
int next;
}edge[2][maxn];
void add(int u,int v,int c){
edge[tmp][cnt].u=u;
edge[tmp][cnt].v=v;
edge[tmp][cnt].cost=c;
edge[tmp][cnt].next=head[tmp][u];
head[tmp][u]=cnt++;
}
void spfa(int s){
queue<int>q;
memset(vis,0,sizeof(vis));
memset(d,INF,sizeof(d));
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[tmp][u];~i;i=edge[tmp][i].next){
Edge e=edge[tmp][i];
if (d[e.v]>e.cost+d[u]){
d[e.v]=e.cost+d[u];
if (!vis[e.v]){
q.push(e.v);
vis[e.v]=1;
}
}
}
}
}
int main() {
//freopen ("in.txt", "r", stdin);
scanf ("%d",&N);
while (N--){
scanf ("%d%d",&n,&m);
for (int i=1;i<=n;i++) head[0][i]=head[1][i]=-1;
int u,v,c;
while (m--){
scanf ("%d%d%d",&u,&v,&c);
tmp=0;
add(u,v,c);
tmp=1;
add(v,u,c);
}
LL ans=0;
tmp=0;
spfa(1);
for (int i=1;i<=n;i++) ans+=d[i];
tmp=1;
spfa(1);
for (int i=1;i<=n;i++) ans+=d[i];
printf ("%lld\n",ans);
}
return 0;
}
相关文章推荐
- POJ 1511 Invitation Cards 求来回最短路(spfa)
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- POJ1511 SPFA模版 用边存的最短路
- POJ 1511 双向单源最短路 SPFA+邻接表
- POJ 1511(邻接表和最短路 SPFA)
- POJ 1511 Invitation Cards(spfa最短路)
- (POJ 1511)Invitation Cards 有向图来回最短路 + SPFA (复习必看题)
- poj 1511 Invitation Cards(spfa最短路)
- POJ-1511 Invitation Cards( 最短路,spfa )
- Invitation Cards POJ - 1511 最短路spfa
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- POJ-1511(SPFA找最短路(正向和反向))
- POJ 1511 最短路spfa
- POJ 1511 Invitation Cards【来回最短路+SPFA】
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
- 【POJ 3259】Wormholes(最短路SPFA/Bellman_Ford)
- poj 3594 Escort of Dr. Who How 最短路spfa
- poj 3255 spfa(求次短路)
- POJ 4046 - Sightseeing(最短路‘spfa)
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*