POJ3268 Silver Cow Party (建反图跑两遍Dij)
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
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <cstring> #include <queue> using namespace std; const int N=1005,M=100010; int head1[N],ver1[M],edge1[M],Next1[M],head2[N],ver2[M],edge2[M],Next2[M],d1[N],d2[N];//建一个原图,一个反图 bool v1[N],v2[N]; int n,m,tot1=0,tot2=0,p; priority_queue<pair<int,int> >q1; priority_queue<pair<int,int> >q2; void add1(int x,int y,int z) { ver1[++tot1]=y; edge1[tot1]=z; Next1[tot1]=head1[x]; head1[x]=tot1; } void add2(int x,int y,int z) { ver2[++tot2]=y; edge2[tot2]=z; Next2[tot2]=head2[x]; head2[x]=tot2; } void dijkstra1() { memset(d1,0x3f,sizeof(d1));//d数组的初始化一定要根据题意,有时要初始化为0,有时初始化为正无穷(最短路)有时初始化为负无穷( dij或者floyd变式求某条最长边) memset(v1,0,sizeof(v1)); d1[p]=0; q1.push(make_pair(0,p)); while(q1.size()) { int x=q1.top().second;q1.pop(); if(v1[x])continue; v1[x]=1; int i; for(i=head1[x];i;i=Next1[i]) { int y=ver1[i],z=edge1[i]; if(d1[y]>d1[x]+z) { d1[y]=d1[x]+z; q1.push(make_pair(-d1[y],y)); } } } } void dijkstra2() { memset(d2,0x3f,sizeof(d2)); memset(v2,0,sizeof(v2)); d2[p]=0; q2.push(make_pair(0,p)); while(q2.size()) { int x=q2.top().second;q2.pop(); if(v2[x])continue; v2[x]=1; int i; for(i=head2[x];i;i=Next2[i]) { int y=ver2[i],z=edge2[i]; if(d2[y]>d2[x]+z) { d2[y]=d2[x]+z; q2.push(make_pair(-d2[y],y)); } } } } int main() { scanf("%d%d%d",&n,&m,&p); int i; for(i=1;i<=m;i++) { int t1,t2,t3; scanf("%d%d%d",&t1,&t2,&t3); add2(t2,t1,t3); add1(t1,t2,t3); } dijkstra1(); dijkstra2(); int ans=0; for(i=1;i<=n;i++) { if(i!=p)ans=max(ans,d1[i]+d2[i]); } cout<<ans; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!