标签:style blog http io ar color os sp for
1511是入门的题目 两次spfa求最小的值。一次正向求1到所有点的最短路,一次反向求最短路 然后两次的答案相加得到答案。
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <queue> 6 7 #define inf 10000000000000LL 8 9 using namespace std; 10 11 const int M = 2000050; 12 13 struct node 14 { 15 int u; 16 int v; 17 long long c; 18 19 }p[M]; 20 21 int Next[M]; 22 int head[M]; 23 long long dp[M]; 24 bool map[M]; 25 int now[M]; 26 27 int e,top,n,m; 28 29 void addnode(int a,int b,long long c) 30 { 31 p[e].c = c; 32 p[e].u = a; 33 p[e].v = b; 34 Next[e] = head[a]; 35 head[a] = e++; 36 37 } 38 39 void init() 40 { 41 memset(head,-1,sizeof(head)); 42 memset(Next,-1,sizeof(Next)); 43 e = 0; 44 for(int i=0;i<m;i++){ 45 int u,v; 46 long long c; 47 scanf("%d%d%lld",&u,&v,&c); 48 addnode(u,v,c); 49 } 50 } 51 52 void un_init() 53 { 54 memset(head,-1,sizeof(head)); 55 memset(Next,-1,sizeof(Next)); 56 for(int i=0;i<m;i++){ 57 addnode(p[i].v,p[i].u,p[i].c); 58 } 59 } 60 61 long long getsum(long long a[]) 62 { 63 long long res = 0; 64 for(int i=1;i<=n;i++){ 65 res += a[i]; 66 } 67 return res; 68 } 69 70 bool relax(int u,int v,long long c) 71 { 72 if(dp[v]> dp[u]+c){ 73 dp[v] = dp[u] + c; 74 return true; 75 } 76 return false; 77 } 78 79 void spfa(int t) 80 { int j; 81 memset(map,0,sizeof(map)); 82 for(int i=1;i<=n;i++){ 83 dp[i] = inf; 84 } 85 dp[t] = 0; 86 map[t] = true; 87 queue <int>Q; 88 Q.push(t); 89 while(!Q.empty()){ 90 int pre = Q.front(); 91 Q.pop(); 92 map[pre] = false; 93 for(int i = head[pre];i+1;i=Next[i] ){ 94 if(relax(pre,p[i].v,p[i].c)&&!map[p[i].v]){ 95 Q.push(p[i].v); 96 map[p[i].v] = true; 97 } 98 } 99 } 100 } 101 102 103 int main () 104 { 105 106 int T ; 107 scanf("%d",&T); 108 while(T--){ 109 scanf("%d%d",&n,&m); 110 init(); 111 spfa(1); 112 long long ans = getsum(dp); 113 un_init(); 114 spfa(1); 115 ans += getsum(dp); 116 printf("%lld\n",ans); 117 } 118 return 0; 119 }
3268完全是1511的一个简单的变种。只是这次要求的是某个点最到所要求的顶点的最短路的最大值。。。完全copy1511的代码然后改了一点,然后就过了...模板大法好啊
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <queue> 6 7 #define inf 10000000000000LL 8 9 using namespace std; 10 11 const int M = 2000050; 12 13 struct node 14 { 15 int u; 16 int v; 17 long long c; 18 19 }p[M]; 20 21 int Next[M]; 22 int head[M]; 23 long long dp[M]; 24 bool map[M]; 25 int now[M]; 26 int d1[M],d2[M]; 27 28 int e,top,n,m; 29 30 void addnode(int a,int b,long long c) 31 { 32 p[e].c = c; 33 p[e].u = a; 34 p[e].v = b; 35 Next[e] = head[a]; 36 head[a] = e++; 37 38 } 39 40 void init() 41 { 42 memset(head,-1,sizeof(head)); 43 memset(Next,-1,sizeof(Next)); 44 e = 0; 45 for(int i=0;i<m;i++){ 46 int u,v; 47 long long c; 48 scanf("%d%d%lld",&u,&v,&c); 49 addnode(u,v,c); 50 } 51 } 52 53 void un_init() 54 { 55 memset(head,-1,sizeof(head)); 56 memset(Next,-1,sizeof(Next)); 57 for(int i=0;i<m;i++){ 58 addnode(p[i].v,p[i].u,p[i].c); 59 } 60 } 61 62 63 bool relax(int u,int v,long long c) 64 { 65 if(dp[v]> dp[u]+c){ 66 dp[v] = dp[u] + c; 67 return true; 68 } 69 return false; 70 } 71 72 void spfa(int t) 73 { int j; 74 memset(map,0,sizeof(map)); 75 for(int i=1;i<=n;i++){ 76 dp[i] = inf; 77 } 78 dp[t] = 0; 79 map[t] = true; 80 queue <int>Q; 81 Q.push(t); 82 while(!Q.empty()){ 83 int pre = Q.front(); 84 Q.pop(); 85 map[pre] = false; 86 for(int i = head[pre];i+1;i=Next[i] ){ 87 if(relax(pre,p[i].v,p[i].c)&&!map[p[i].v]){ 88 Q.push(p[i].v); 89 map[p[i].v] = true; 90 } 91 } 92 } 93 } 94 95 int main () 96 { 97 98 int T ; 99 100 scanf("%d%d%d",&n,&m,&T); 101 init(); 102 spfa(T); 103 104 105 memset(d1,0,sizeof(d1)); 106 memset(d2,0,sizeof(d2)); 107 for(int i=1;i<=n;i++){ 108 d1[i] = dp[i]; 109 } 110 un_init(); 111 spfa(T); 112 long long res2 = 0; 113 for(int i=1;i<=n;i++){ 114 d2[i] = dp[i]; 115 116 } 117 int ans = 0; 118 for( int i=1;i<=n;i++){ 119 ans = max(ans,(d1[i]+d2[i])); 120 } 121 printf("%d\n",ans); 122 123 return 0; 124 }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/lmlyzxiao/p/4127212.html