POJ 3613Cow Relays (Floyd multiplication method)

Source: Internet
Author: User

Test instructions: What is the shortest path from the s point to the E-point and the N-side (can walk the repeated paths)

Figure Midpoint <=200,n<=1000,000


Idea: Folyd can be implemented to add edge to the path, but this problem and common to find the shortest-circuiting problems, such as from S to e after X-side has reached the shortest, this time still forced to use Folyd Tim Edge, although Tim Edge is not the shortest, But note that the added side to minimize the shortest possible loss, seize this with FOLYD can be implemented to enforce the edge of the operation, so you can move from the state of N=1 to N state


So the original map can be n-1 times folyd, but the range of n is too large, the worst case to be 1000000*200^3 operations will definitely t


At this point, notice that the multiplication method can be used to accelerate the transfer of the state, any n can be decomposed into 2^i and, each 2^i state can be directly obtained from the state of 2^ (I-1).

That is, the distance between each point in the current map is represented by the 2^ (i-1) edge, and then Folyd once for the current map, you can calculate the shortest path between each point in the map through the 2^ (i-1) +2^ (i-1) =2^i bar

So the multiplication method accelerates the state transition.

628k94ms#include<cstdio> #include <algorithm> #include <cstring> #include <iostream>using namespace std; #define INF 0x3f3f3f3f#define M 205int n,m,st,ed;int hash[1005],cnt;int mapp[m][m],tmp[m][m],n_ans[m][m] void Folyd (int a[m][m],int b[m][m],int c[m][m]) {for (Int. k=1;k<=cnt;k++) for (int. j=1;j<=cnt;j++) for (int i =1;i<=cnt;i++) if (A[j][i]>b[j][k]+c[k][i]) a[j][i]=b[j][k]+c[k][i];} void map_copy (int a[m][m],int b[m][m]) {for (Int. i=1;i<=cnt;i++) for (int j=1;j<=cnt;j++) {a[i][j]=b[i][j        ];    B[i][j]=inf;    }}int Main () {memset (mapp,0x3f,sizeof (MAPP));    memset (tmp,0x3f,sizeof (TMP));    memset (n_ans,0x3f,sizeof (N_ans));        for (int i=1;i<=200;i++) {n_ans[i][i]=0;        Be careful not to let mapp[i][i]=tmp[i][i]=0, because to make tmp[i][i] any time infinite because itself to itself also through the other multiple cattle//mapp[i][j] is not the value of the INF means that there must be an edge, cannot assume itself to itself is a right 0 side    N_ans[i][i] Initialize itself to 0 is the boundary condition of dynamic Programming} scanf ("%d%d%d%d", &n,&m,&st,&ed); while (m--){int val,u,v;        scanf ("%d%d%d", &val,&u,&v); if (!        Hash[u]) {//vertex discretization hash[u]=++cnt; } if (!        Hash[v]) {hash[v]=++cnt;     } Mapp[hash[u]][hash[v]]=mapp[hash[v]][hash[u]]=val;            } while (n) {if (n&1) {folyd (Tmp,n_ans,mapp);        Map_copy (N_ANS,TMP);        } folyd (Tmp,mapp,mapp);        Map_copy (MAPP,TMP); n>>=1;    Accelerated State Transfer} printf ("%d\n", N_ans[hash[st]][hash[ed]); return 0;}


POJ 3613Cow Relays (Floyd multiplication method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.