poj 3268(spfa)

http://poj.org/problem?id=3268

对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS。

感觉人生观都奔溃了,果然,题目做多了总会见到鬼的!!!!!!

心累,不想写这个题了。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 #include <iostream>
 5 #define inf 0x3f
 6 #define Max1 1010
 7 #define Max2 100010
 8
 9 using namespace std;
10 int m,n,x;
11 int mark[Max1];
12 int next[Max1];
13 int u[Max2];                       //把13和14行交换一下,你得到了结果会完全不同。。
14 int v[Max2];
15 int w[Max2];
16 int a[Max2],b[Max2],c[Max2];
17 int dist[Max1];
18 int ans[Max1];
19 bool vis[Max1];
20
21
22 queue <int >s;
23 void init()
24 {
25     memset( vis,false,sizeof(vis));
26     while( !s.empty()) s.pop();
27     memset(dist,inf,sizeof( dist ));
28     dist[x]=0;
29     for(int i=1;i<=n;i++)
30     mark[i]=-1;
31 }
32
33 void spfa()
34 {
35     s.push( x );
36     vis[ x ] = true ;
37     while( !s.empty() )
38     {
39         int tmp = s.front();
40         s.pop();
41         vis[ tmp ] = false;
42         for(int i = mark[ tmp ] ; i != -1 ; i = next[ i ] )
43         {
44             if( dist[ tmp ] + w[ i ] < dist[ v[ i ] ])
45             {
46                 dist[ v[ i ] ] = dist[ tmp ] + w[ i ];
47                 if( !vis[ v[ i ] ] )
48                 {
49                     s.push( v[ i ] );
50                     vis[ v[ i ] ] = true;
51                 }
52             }
53         }
54     }
55     for( int i = 1 ; i <= n ; i++ )
56         ans[ i ] += dist[ i ];
57 }
58
59 int main()
60 {
61  //   freopen("in.txt","r",stdin);
62     while(~scanf("%d%d%d",&n,&m,&x))
63     {
64         memset( ans , 0 , sizeof( ans ) );
65         init();
66         for( int i = 1 ; i <= m ; i++ )
67         {
68             scanf( "%d%d%d",&a[ i ],&b[ i ],&c[ i ] );
69             u[ i ] = a[ i ];
70             v[ i ] = b[ i ];
71             w[ i ] = c[ i ];
72             next[ i ] = mark [ u[ i ] ] ;
73             mark[ u[ i ] ] = i;
74         }
75         spfa();
76         init();
77         for( int i = 1 ; i <= m ; i++ )
78         {
79             v[ i ] = a[ i ];
80             u[ i ] = b[ i ];
81             w[ i ] = c[ i ];
82             next[ i ] = mark[ u[ i ] ];
83             mark[ u[ i ] ] = i;
84         }
85         spfa();
86         int Max = ans[1];
87         for( int i = 2 ; i <= n ; i++ )
88             if( ans[ i ] > Max ) Max = ans[ i ];
89         printf("%d\n",Max);
90     }
91     return 0;
92 }
时间: 2024-10-17 04:36:02

poj 3268(spfa)的相关文章

POJ 3268 Silver Cow Party(SPFA)

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 re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

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 

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party 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

POJ 3268 双向Dijkstra

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13020 Accepted: 5832 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)

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

poj 3259Wormholes (spfa最短路径)

#include<stdio.h> #include<string.h> #include<limits.h> #include<queue> using namespace std; #define N 5505 #define M 55000//注意边和点集的数组大小 struct edge { int to,value,next; }edges[M]; int heads[N],len=0; int addedge(int u,int v,int w)