POJ--3268--Silver Cow Party【SPFA+邻接表】
题意:一些牛要去某一点參加聚会,然后再回到自己家,路是单向的,问花费时间最多的那头牛最少须要花费多长时间。
思路:从聚会地点返回,相当于是从某一点到其它各个点的最短路径。从牛的家中走到聚会地点,能够把路径反过来变成从聚会地点到各个点的最短路径,两个最短路径值加起来就是每头牛所花费的最小时间,找出最大的就可以。
我用了两个邻接表存路径,事实上这道题用邻接矩阵存更好做,矩阵横纵坐标翻转就把路径反转了,我用SPFA写想练练手,一直都不会手写SPFA,做几道题找找感觉。
AC竟然用时0MS。。
#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 100100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct node{
int u,v,next;
}edge[MAXN],redge[MAXN];
int head[MAXN],rhead[MAXN],dist[MAXN],vis[MAXN],ans[MAXN];
int cnt,rcnt,n,m,x;
void add_edge(int a,int b,int c){
edge[cnt].u = b;
edge[cnt].v = c;
edge[cnt].next = head[a];
head[a] = cnt++;
redge[rcnt].u = a;
redge[rcnt].v = c;
redge[rcnt].next = rhead[b];
rhead[b] = rcnt++;
}
void spfa(int type){
int i,j;
for(i=1;i<=n;i++){
dist[i] = INF;
}
dist[x] = 0;
memset(vis,0,sizeof(vis));
vis[x] = 1;
queue<int>q;
q.push(x);
while(!q.empty()){
int temp = q.front();
q.pop();
vis[temp] = 0;
for(i=type?rhead[temp]:head[temp];i!=-1;i=type?redge[i].next:edge[i].next){
int lu = type?redge[i].v:edge[i].v;
if(lu+dist[temp]<dist[type?redge[i].u:edge[i].u]){
dist[type?redge[i].u:edge[i].u] = lu + dist[temp];
if(!vis[type?redge[i].u:edge[i].u]){
vis[type?redge[i].u:edge[i].u] = 1;
q.push(type?redge[i].u:edge[i].u);
}
}
}
}
}
int main(){
int i,j,a,b,c;
scanf("%d%d%d",&n,&m,&x);
memset(head,-1,sizeof(head));
memset(rhead,-1,sizeof(rhead));
cnt = rcnt = 0;
int maxm = 0;
for(i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
spfa(0);
for(i=1;i<=n;i++){
if(i!=x) ans[i] = dist[i];
}
spfa(1);
for(i=1;i<=n;i++){
if(i!=x) ans[i] += dist[i];
if(ans[i]>maxm) maxm = ans[i];
}
printf("%d\n",maxm);
return 0;
}
POJ--3268--Silver Cow Party【SPFA+邻接表】的更多相关文章
- POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路
Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
随机推荐
- redisTemplate 操作
redisDao封装类-其他dao集成他 package com.ffcs.wlan.dao.common; import javax.annotation.Resource; import org. ...
- Swift初体验 (一)
// 声明一个常量 let maxNumberOfStudents: Int = 47 // 声明一个变量,假设没有在声明的时候初始化,须要显示的标注其类型 var currentNumberOfSt ...
- SQL SERVER 2008- 字符串函数
/* 1,ASCII返回字符表达式中最左侧字符的ASCII代码值 仅返回首字母的ASCII码值 parameter char或varchar returns integer */ SELECT ASC ...
- vs2010中将复制过来的文件或文件夹显示到解决方案管理
今天在给一个做好的页面上加.net程序,我先将程序中的文件夹复制到解决方案中,可是在VS2010的解决方案资源管理器中并没有这样的文件夹,可明明 在这里,为什么显示不出来,应该在VS2010的哪个地方 ...
- 初入Android--环境搭建
Android SDK 可以下载adt-bundle:包含了装好插件的eclipse和android sdk.下载好后,首先设置ANDROID_HOME环境变量:ANDROID_HOME=/home/ ...
- 1352 - Colored Cubes (枚举方法)
There are several colored cubes. All of them are of the same size but they may be colored differentl ...
- Eclipse设置Android Logcat输出字体大小
Window -> Preferences -> Android -> Logcat -> Display Font:点击"Change"button 如图 ...
- How to find configuration file MySQL uses?(转)
http://www.dbasquare.com/2012/04/01/how-to-find-mysql-configuration-file/ A customer called me today ...
- 5.中文问题(自身,操作系统级别,应用软件的本身),mysql数据库备份
第一层因素: mysql的自身的设置 mysql有六处使用了字符集.分别为:client .connection.database.results.server .system. mysql&g ...
- ios点击产生波纹效果
ios点击产生波纹效果 by 伍雪颖 - (void)viewDidLoad { [super viewDidLoad]; RippleView = [[UIView alloc] initWithF ...