POJ 1511 Invitation Cards 最短路SPFA
2015-10-05 19:54
302 查看
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
要求1到其他所有节点一去一回的最小权值
求来回直接把边反向,SPFA两次
数据有点大,要用longlong
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
struct Node
{
int u,w,nt;
}f[2][1000009];
int head[2][1000009];
int dis[1000009];
int n,m;
queue<int>q;
int vis[1000009];
void spfa(int kk)
{
for(int i=0;i<=n;i++)
{
vis[i]=0;
dis[i]=INF;
}
dis[1]=0;
vis[1]=1;
q.push(1);
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=1;
for(int i=head[kk][x];i!=-1;i=f[kk][i].nt)
{
if(dis[f[kk][i].u]>dis[x]+f[kk][i].w)
{
dis[f[kk][i].u]=dis[x]+f[kk][i].w;
if(!vis[f[kk][i].u])
{
vis[f[kk][i].u]=0;
q.push(f[kk][i].u);
}
}
}
}
}
int main()
{
int T;
int u,v,w;
int cnt;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
cnt=0;
for(int i=0;i<=n;i++)
{
head[0][i]=-1;
head[1][i]=-1;
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
f[0][cnt].u=u;
f[0][cnt].w=w;
f[0][cnt].nt=head[0][v];
head[0][v]=cnt;
f[1][cnt].u=v;
f[1][cnt].w=w;
f[1][cnt].nt=head[1][u];
head[1][u]=cnt;
cnt++;
}
__int64 ans=0;
spfa(0);
for(int i=1;i<=n;i++)
ans+=dis[i];
spfa(1);
for(int i=1;i<=n;i++)
ans+=dis[i];
printf("%I64d\n",ans);
}
return 0;
}
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 22633 | Accepted: 7409 |
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
要求1到其他所有节点一去一回的最小权值
求来回直接把边反向,SPFA两次
数据有点大,要用longlong
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
struct Node
{
int u,w,nt;
}f[2][1000009];
int head[2][1000009];
int dis[1000009];
int n,m;
queue<int>q;
int vis[1000009];
void spfa(int kk)
{
for(int i=0;i<=n;i++)
{
vis[i]=0;
dis[i]=INF;
}
dis[1]=0;
vis[1]=1;
q.push(1);
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=1;
for(int i=head[kk][x];i!=-1;i=f[kk][i].nt)
{
if(dis[f[kk][i].u]>dis[x]+f[kk][i].w)
{
dis[f[kk][i].u]=dis[x]+f[kk][i].w;
if(!vis[f[kk][i].u])
{
vis[f[kk][i].u]=0;
q.push(f[kk][i].u);
}
}
}
}
}
int main()
{
int T;
int u,v,w;
int cnt;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
cnt=0;
for(int i=0;i<=n;i++)
{
head[0][i]=-1;
head[1][i]=-1;
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
f[0][cnt].u=u;
f[0][cnt].w=w;
f[0][cnt].nt=head[0][v];
head[0][v]=cnt;
f[1][cnt].u=v;
f[1][cnt].w=w;
f[1][cnt].nt=head[1][u];
head[1][u]=cnt;
cnt++;
}
__int64 ans=0;
spfa(0);
for(int i=1;i<=n;i++)
ans+=dis[i];
spfa(1);
for(int i=1;i<=n;i++)
ans+=dis[i];
printf("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- STL源码学习----lower_bound和upper_bound算法
- VS2010播放AVI视频时一闪而过的解决办法
- Oracle外键(Foreign Key)使用详细的说明(一)
- 指针间接赋值
- CYC-AVPlayer播放器的简单运用-02
- 51nod_learn_greedy_独木舟问题
- cmd无法使用粘贴的设置
- 读《暗时间》有感(逐步更新)
- Kettle 学习笔记
- android自定义组件的简易实现
- ScrollView测量分析
- android自定义组件的简易实现
- hadoop的数据类型的应用
- CComboBox使用技巧
- bzoj2142
- Linux内核模块指南(第六章===>第八章完)。。。翻译完。。。
- Lucene-学习笔记 (版本3,5VS 5.3)
- bzoj2142
- 欢迎使用CSDN-markdown编辑器
- 电话短信和邮件