图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12674 | Accepted: 5651 |
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
Source
Mean:
原意:草场上有n个农场,农场之间有一些路径,每个农场里住着一头牛,现在x农场的牛要过生日开party,其他农场的牛要到该农场去参加party,现在让你选择一头来回耗时最多的一头牛出来,输出时间。
给你一个n个结点、m条边的有向图,现在要你求从n-1个结点到达指定的一个结点的来回最长路。
analyse:
这题思路很巧妙,我们在存图的时候用链式向前星来存,存的时候就建两次边,一次正向,一次反向,用一个flag来标记一下。然后用两遍spfa,第一遍求出从x点出发的正向图到每个结点的最短路,第二遍求出从x点出发的反向图到每个结点的最短路,最后将两次的最短路对应相加,求出最大值即为最终的answer。
Time complexity:O(n*k)
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | //Memory Time // 3521K 241MS //by : Snarl_jsb #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<iomanip> #include<string> #include<climits> #include<cmath> #define MAXV 1010 #define MAXE 100010 #define LL long long using namespace std; namespace Adj { struct Node { int to,next,val; bool flag; } edge[MAXE<<1]; int top,head[MAXV]; void init() { top=1; memset (head,0, sizeof (head)); } void addEdge( int u, int v, int val) { edge[top].to=v; edge[top].val=val; edge[top].flag=1; edge[top].next=head[u]; head[u]=top++; edge[top].to=u; edge[top].val=val; edge[top].flag=0; edge[top].next=head[v]; head[v]=top++; } } using namespace Adj; int n,m,x,ans; bool vis[MAXV]; int dis[MAXV]; int dis1[MAXV]; void spfa( bool flag) { memset (vis,0, sizeof (vis)); for ( int i=0;i<=n;i++) dis[i]=INT_MAX; queue< int >Q; Q.push(x); vis[x]=1; dis[x]=0; while (!Q.empty()) { int now=Q.front(); Q.pop(); vis[now]=0; for ( int i=head[now];i;i=edge[i].next) { if (flag==1) { if (edge[i].flag==0) continue ; } else { if (edge[i].flag==1) continue ; } int son=edge[i].to; int val=edge[i].val; if (dis[now]+val<dis[son]) { dis[son]=dis[now]+val; if (!vis[son]) { vis[son]=1; Q.push(son); } } } } if (flag==1) { for ( int i=1;i<=n;i++) dis1[i]=dis[i]; } else { int Max=INT_MIN; for ( int i=1;i<=n;i++) { dis[i]+=dis1[i]; if (dis[i]>Max) Max=dis[i]; } ans=Max; } } int main() { // freopen("cin.txt","r",stdin); // freopen("cout.txt","w",stdout); while (~ scanf ( "%d %d %d" ,&n,&m,&x)) { init(); int u,v,w; while (m--) { scanf ( "%d %d %d" ,&u,&v,&w); addEdge(u,v,w); } spfa(1); spfa(0); printf ( "%d\n" ,ans); } return 0; } |
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3898257.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?