POJ-3268
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13738 | Accepted: 6195 |
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
/**
题意:最短路中哪个走的路程最大
解法:SPFA
**/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define maxn 1000 + 10
#define INF 0x3f3f3f3f
using namespace std;
int vis[maxn];
int dist[maxn];
int dist1[maxn];
int flag[maxn];
struct Node
{
int v;
int cost;
Node(int _v,int _cost):v(_v),cost(_cost) {}
};
vector<Node>edge[maxn];
vector<Node>edge1[maxn];
int n,m,p;
void addedge(int u,int v,int w)
{
edge[u].push_back(Node(v,w));
edge1[v].push_back(Node(u,w));
}
void SPFA(int start)
{
memset(vis,,sizeof(vis));
memset(dist,INF,sizeof(dist));
queue<int>que;
que.push(start);
vis[start] = ;
dist[start] = ;
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge[tt].size(); i++)
{
int mm = edge[tt][i].v;
if(dist[mm] > dist[tt] + edge[tt][i].cost)
{
dist[mm] = dist[tt] + edge[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
memset(vis,,sizeof(vis));
memset(dist1,INF,sizeof(dist1));
while(!que.empty()) que.pop();
vis[start] = ;
dist1[start] = ;
que.push(start);
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge1[tt].size(); i++)
{
int mm = edge1[tt][i].v;
if(dist1[mm] > dist1[tt] + edge1[tt][i].cost)
{
dist1[mm] = dist1[tt] + edge1[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
return ;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
scanf("%d %d %d",&n,&m,&p);
int u,v,w;
for(int i=; i<m; i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
SPFA(p);
int mmax = -INF;
for(int i=; i<=n; i++)
{
if(dist[i] + dist1[i] > mmax && dist[i] != INF && dist1[i]!= INF )
mmax = dist[i] + dist1[i];
}
printf("%d\n",mmax);
return ;
}
/**
题意:最短路中哪个走的路程最大
解法:SPFA
**/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define maxn 1000 + 10
#define INF 0x3f3f3f3f
using namespace std;
int vis[maxn];
int dist[maxn];
int dist1[maxn];
int flag[maxn];
struct Node
{
int v;
int cost;
Node(int _v,int _cost):v(_v),cost(_cost) {}
};
vector<Node>edge[maxn];
vector<Node>edge1[maxn];
int n,m,p;
void addedge(int u,int v,int w)
{
edge[u].push_back(Node(v,w));
edge1[v].push_back(Node(u,w));
}
void SPFA(int start)
{
memset(vis,,sizeof(vis));
memset(dist,INF,sizeof(dist));
queue<int>que;
que.push(start);
vis[start] = ;
dist[start] = ;
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge[tt].size(); i++)
{
int mm = edge[tt][i].v;
if(dist[mm] > dist[tt] + edge[tt][i].cost)
{
dist[mm] = dist[tt] + edge[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
memset(vis,,sizeof(vis));
memset(dist1,INF,sizeof(dist1));
while(!que.empty()) que.pop();
vis[start] = ;
dist1[start] = ;
que.push(start);
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge1[tt].size(); i++)
{
int mm = edge1[tt][i].v;
if(dist1[mm] > dist1[tt] + edge1[tt][i].cost)
{
dist1[mm] = dist1[tt] + edge1[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
return ;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
scanf("%d %d %d",&n,&m,&p);
int u,v,w;
for(int i=; i<m; i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
SPFA(p);
int mmax = -INF;
for(int i=; i<=n; i++)
{
if(dist[i] + dist1[i] > mmax && dist[i] != INF && dist1[i]!= INF )
mmax = dist[i] + dist1[i];
}
printf("%d\n",mmax);
return ;
}
/**
题意:最短路中哪个走的路程最大
解法:SPFA
**/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define maxn 1000 + 10
#define INF 0x3f3f3f3f
using namespace std;
int vis[maxn];
int dist[maxn];
int dist1[maxn];
int flag[maxn];
struct Node
{
int v;
int cost;
Node(int _v,int _cost):v(_v),cost(_cost) {}
};
vector<Node>edge[maxn];
vector<Node>edge1[maxn];
int n,m,p;
void addedge(int u,int v,int w)
{
edge[u].push_back(Node(v,w));
edge1[v].push_back(Node(u,w));
}
void SPFA(int start)
{
memset(vis,,sizeof(vis));
memset(dist,INF,sizeof(dist));
queue<int>que;
que.push(start);
vis[start] = ;
dist[start] = ;
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge[tt].size(); i++)
{
int mm = edge[tt][i].v;
if(dist[mm] > dist[tt] + edge[tt][i].cost)
{
dist[mm] = dist[tt] + edge[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
memset(vis,,sizeof(vis));
memset(dist1,INF,sizeof(dist1));
while(!que.empty()) que.pop();
vis[start] = ;
dist1[start] = ;
que.push(start);
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge1[tt].size(); i++)
{
int mm = edge1[tt][i].v;
if(dist1[mm] > dist1[tt] + edge1[tt][i].cost)
{
dist1[mm] = dist1[tt] + edge1[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
return ;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
scanf("%d %d %d",&n,&m,&p);
int u,v,w;
for(int i=; i<m; i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
SPFA(p);
int mmax = -INF;
for(int i=; i<=n; i++)
{
if(dist[i] + dist1[i] > mmax && dist[i] != INF && dist1[i]!= INF )
mmax = dist[i] + dist1[i];
}
printf("%d\n",mmax);
return ;
}
POJ-3268的更多相关文章
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- 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、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- 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(spfa)
http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj - 3268 Silver Cow Party (求给定两点之间的最短路)
http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...
- <poj - 3268> Silver Cow Party 牛のpart 最短路径问题
本题链接 : http://poj.org/problem?id=3268 题目大意:牛们要去聚会,输入N = 顶点数(牛场):M = 边(路)的数目: X = 终点 (聚会点).问题:求来回时间的最 ...
- POJ 3268 (dijkstra变形)
题目链接 :http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveni ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
随机推荐
- javascript实用例子
js学习笔记,别错过!很有用的. /////////////////////////////////////////////////////////////////////////////////// ...
- mysql的cast()函数
先来补个知识: decimal的用法: decimal(18,4)总长18位,包括1位小数点和4为小数,也就是说18-1-4=13整数位只有13位decimal(19,1)总长19位,17位整数,1位 ...
- maven的tomcat插件如何进行debug调试
利用maven来部署工程时,一般采用的是tomcat插件,使项目在tomcat上面运行,那么这个debug调试是如何进行呢? 我们在调试的时候问题: 会提示找不到资源,那么如何进行修改呢,方法两个: ...
- [技巧篇]21.Android Studio的快捷键设置[图片版]
如果对你有帮助,请点击推荐!
- linux下输出查看进程及杀进程
1.查找有关tomcat的进程 ps -ef | grep tomcat 2.查看某端口占用情况 netstat -tulpn | grep 9009 3.杀进程 普通:kill 进程id 强制:ki ...
- Spring Security 集成CAS实现单点登录
参考:http://elim.iteye.com/blog/2270446 众所周知,Cas是对单点登录的一种实现.本文假设读者已经了解了Cas的原理及其使用,这些内容在本文将不会讨论.Cas有Ser ...
- AWK文本分析工具-常用场景(持续更新中)
AWK help document:http://www.gnu.org/software/gawk/manual/gawk.html 问题 awk命令 备注 对请求IP统计分组排序? 显示列 ...
- Android中Handler导致的内存泄露
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html Consider the follo ...
- 【BZOJ】3895: 取石子
[算法]博弈论+记忆化搜索 [题意]给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 [题解] 首先,若所有石子堆的石子数>1,显然总操作数为(石子 ...
- 12.13记录//QQDemo示例程序源代码
笔记的完整版pdf文档下载地址: https://www.evernote.com/shard/s227/sh/ac692160-68c7-4149-83ea-0db5385e28b0 ...