poj 1511 SPFA
2016-08-06 15:20
351 查看
Invitation Cards
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
Sample Output
题意:
有很多个人要走向P个点,然后又Q条有向路径
求这些人分别去这些点,并且回来最短的路径(价格)总和
题解:
SPFA裸题
去的时候当然从1出发,计算到所有点距离的总和
回来的时候可以看做路径相反,依然从点 1出发,计算最短路径
注意:
Prices are positive integers the sum of which is smaller than 1000000000
每一个price就已经很大了,所以ans需要用long long 型
WA了一次,因为输出的格式不对,以后注意一下就好了
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 1000010
#define INF 0x3f3f3f3f
int n,m;
struct node
{
int u,v,w;
}a[MAXN];
struct ways
{
int v,w,next;
}path[MAXN];
int top;
int vis[MAXN],head[MAXN];
int dis[MAXN];
void add(int u,int v,int w)
{
path[top].v=v;
path[top].w=w;
path[top].next=head[u];
head[u]=top++;
}
void SPFA()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
dis[i]=INF;
queue<int>q;
q.push(1);
dis[1]=0;
while(q.size())
{
int p=q.front();
q.pop();
vis[p]=0;
for(int i=head[p];i!=-1;i=path[i].next){
int t=path[i].v;
if(dis[t]>dis[p]+path[i].w){
dis[t]=dis[p]+path[i].w;
if(vis[t]==0){
q.push(t);
vis[t]=1;
}
}
}
}
}
int main()
{
int T;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
long long ans=0;
top=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++){
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
add(a[i].u,a[i].v,a[i].w);
}
SPFA();
for(int i=1;i<=n;i++)
ans+=dis[i];
top=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
add(a[i].v,a[i].u,a[i].w);
SPFA();
for(int i=1;i<=n;i++)
ans+=dis[i];
printf("%lld\n",ans);
}
return 0;
}
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 25057 | Accepted: 8273 |
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
题意:
有很多个人要走向P个点,然后又Q条有向路径
求这些人分别去这些点,并且回来最短的路径(价格)总和
题解:
SPFA裸题
去的时候当然从1出发,计算到所有点距离的总和
回来的时候可以看做路径相反,依然从点 1出发,计算最短路径
注意:
Prices are positive integers the sum of which is smaller than 1000000000
每一个price就已经很大了,所以ans需要用long long 型
WA了一次,因为输出的格式不对,以后注意一下就好了
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 1000010
#define INF 0x3f3f3f3f
int n,m;
struct node
{
int u,v,w;
}a[MAXN];
struct ways
{
int v,w,next;
}path[MAXN];
int top;
int vis[MAXN],head[MAXN];
int dis[MAXN];
void add(int u,int v,int w)
{
path[top].v=v;
path[top].w=w;
path[top].next=head[u];
head[u]=top++;
}
void SPFA()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
dis[i]=INF;
queue<int>q;
q.push(1);
dis[1]=0;
while(q.size())
{
int p=q.front();
q.pop();
vis[p]=0;
for(int i=head[p];i!=-1;i=path[i].next){
int t=path[i].v;
if(dis[t]>dis[p]+path[i].w){
dis[t]=dis[p]+path[i].w;
if(vis[t]==0){
q.push(t);
vis[t]=1;
}
}
}
}
}
int main()
{
int T;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
long long ans=0;
top=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++){
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
add(a[i].u,a[i].v,a[i].w);
}
SPFA();
for(int i=1;i<=n;i++)
ans+=dis[i];
top=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
add(a[i].v,a[i].u,a[i].w);
SPFA();
for(int i=1;i<=n;i++)
ans+=dis[i];
printf("%lld\n",ans);
}
return 0;
}
相关文章推荐
- 捕获2-频率损耗因子
- android sdk里的各目录作用
- 可变参数:定义方法的时候不知道该定义多少个参数
- POJ 1860 Bellman改判断正环
- POJ 3628 01背包 OR 状压
- 偏好设置
- POJ 3628 01背包 OR 状压
- 用户 组 和权限 以及权限的 分类
- 机器学习实战(一)
- hdu 1226(同余搜索)
- 双ListView与顶部标题栏滑动事件处理(仿美团外卖商家详情界面滑动效果)
- 小四轴DIY笔记之time定时器初始化
- HDU 1525 Euclid's Game (博弈、找规律)*
- ASP.Net 跨域跳转域名 使用jquery ajax请求
- python基础学习
- 基于腾讯云个人博客的搭建
- iOS 多线程开发小结
- Redis 命令操作
- CSDN上如何转载
- 在内核里写i2c client 驱动的两种方式