(简单) POJ 1511 Invitation Cards,SPFA。
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.
题目是最短路问题,求源点到所有点的来回的最短距离之和,只需要反向边图+正向边图即可。。。
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int INF = 10e8;
const int MaxN = ; struct Edge
{
int v, cost,next;
}; Edge E1[MaxN],E2[MaxN],*E;
int head1[MaxN],head2[MaxN],Ecou1,Ecou2;
int *head;
bool vis[MaxN];
int couNode[MaxN]; bool SPFA(long long lowcost[], int n, int start,int type)
{
if(type)
E=E1,head=head1;
else
E=E2,head=head2; queue <int> que;
int u, v, c;
int len; for (int i = ; i <= n; ++i) { lowcost[i] = INF; vis[i] = ; couNode[i] = ; } vis[start] = ; lowcost[start] = ; couNode[start] = ;
que.push(start); while (!que.empty())
{
u = que.front(); que.pop(); vis[u] = ; for (int i = head[u]; i!=-; i=E[i].next)
{
v = E[i].v; c = E[i].cost; if (lowcost[v]>lowcost[u] + c)
{
lowcost[v] = lowcost[u] + c; if (!vis[v])
{
vis[v] = ; ++couNode[v]; que.push(v); if (couNode[v]>n) return ;
}
}
}
} return ;
} inline void addEdge(int u, int v, int c,int type)
{
int *Ecou;
if(type)
{
E=E1,head=head1;
Ecou=&Ecou1;
}
else
{
E=E2,head=head2;
Ecou=&Ecou2;
} E[*Ecou].v=v;
E[*Ecou].cost=c;
E[*Ecou].next=head[u];
head[u]=(*Ecou)++;
} void init(int N)
{
Ecou1=Ecou2=; for(int i=;i<=N;++i)
head1[i]=head2[i]=-;
} long long ans1[MaxN],ans2[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T;
int N,M;
int a,b,c;
long long ans; cin>>T; while(T--)
{
scanf("%d %d",&N,&M);
init(N); for(int i=;i<=M;++i)
{
scanf("%d %d %d",&a,&b,&c);
addEdge(a,b,c,);
addEdge(b,a,c,);
} SPFA(ans1,N,,);
SPFA(ans2,N,,); ans=;
for(int i=;i<=N;++i)
ans+=ans1[i]+ans2[i]; cout<<ans<<endl;
} return ;
}
(简单) POJ 1511 Invitation Cards,SPFA。的更多相关文章
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- poj 1511 Invitation Cards spfa 邻接矩阵
题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- POJ 1511 Invitation Cards(逆向思维 SPFA)
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
随机推荐
- wpf之数据触发器DataTrigger
wpf, 根据绑定的属性的值的不同(数据分类),界面上显示不同的控件(绑定不同类型的属性),可以使用数据库触发器DataTrigger实现这一功能. 实现的效果如下: 首先建立实体类: 更改通知类: ...
- 转 vi 技巧和诀窍:令人刮目相看的 10 个超酷命令
for ksh vi 编辑器的许多选项可以控制编辑会话的外观和感觉.使用 :set 命令修改 vi 中的会话设置.按 Escape 键进入命令模式之后,可以使用 :set all 命令显示选项和设置的 ...
- 【sort】 基数排序
下面这段问答摘自csdn: 把基数排序说成桶排序应该是没有太大问题的.总的说来,应该把这一类归为分配排序,由于分配排序的一些缺陷,主要是时间代价很差,改进成为桶式排序(bucket sort),而桶排 ...
- C#之控制台输入和输出
控制台输出 C# 控制台程序一般使用 .NET Framework Console 类提供的输入/输出服务.Console.WriteLine("Hello World!"); 语 ...
- Ugly Problem
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Spec ...
- nand驱动移植
首先下载nand flash驱动 s3c_nand.c ,此文件包含着nand flash驱动具体的实现,将其复制到drivers/mtd/nand下: s3c_nand.c 下载地址 s3c_nan ...
- HDU 4635 Strongly connected(强连通分量缩点+数学思想)
题意:给出一个图,如果这个图一开始就不是强连通图,求出最多加多少条边使这个图还能保持非强连通图的性质. 思路:不难想到缩点转化为完全图,然后找把它变成非强连通图需要去掉多少条边,但是应该怎么处理呢…… ...
- HDU 1686 Oulipo(KMP+计算匹配成功次数)
一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊 原串为ABABA,模式 ...
- This compilation unit is not on the build path SVN
This compilation unit is not on the build path of a Java project 解决办法 把项目导入STS(基于Eclipse)时,项目出现问题, ...
- 使用log4j无法输出日志
前段时间在项目的过程中使用log4j来输出日志,但是在一个项目里我明明已经在src/main/resource目录下创建了log4j.properties.具体配置如下: log4j.rootLogg ...