The main idea: to give a nxn matrix, to find a nxn containing only 0 and 1 of the Matrix, so that two matrix position corresponding to the elements of the multiplication and the smallest.
Ideas: Ingenious ideas, the three conditions given by the topic can be converted into a problem of finding the shortest path by considering the angle of graph theory.
A new question was learned from this question – "0/1 planning".
Various types of puzzle:
Official
Kuangbin
Cxlove
Code:
/* W w w mm mm 222222222 7777777777777 *//* W w W W m M M 222 7777 *//* W w w w m m M m 777 *//* W w w w m M m * *///w W w m M M 222 * *//* W w w m M M 222 *//* W W W m M M M 222 *//* W w w W M M M 222 *///* W w w m M m M 222 */* ww ww m mm m 222222222222222
77 */
#pragma COMMENT (linker, "/stack:102400000,102400000")//c++//int size = << 20;
256MB//char *p = (char*) malloc (size) + size;
__asm__ ("Movl%0,%%esp\n":: "R" (p)); g++ #include <set> #include <map> #include <queue> #include <stack> #include <ctime> # include<deque> #include <cmath> #include <vector> #include <string> #include <cctype> # include<cstdio> #include <cstdlib> #include <cstring> #include <sstream> #include <iostream > #include <algorithm> #define REP (i,s,t) for (int i= (s), i<= (t); i++) #define REP2 (i,t,s) for (int i= (t); i>=
s;i--) using namespace std;
typedef long Long LL;
typedef unsigned long long ull;
typedef unsigned long UL;
int n;
const int maxn=305;
const int inf=0x3f3f3f3f;
int PATH[MAXN][MAXN];
BOOL vis[maxn];//in queue flag int DIST[MAXN];
void SPFA (int start,int n) {memset (vis,false,sizeof (VIS));
queue<int>q; for (int i=1; i<=n; i++) {iF (i==start) {dist[i]=inf;
} else {dist[i]=path[start][i];
Q.push (i);
Vis[i]=1; }} while (!
Q.empty ()) {int U=q.front ();
Q.pop ();
vis[u]=0; REP (i,1,n) {if (Dist[i]>dist[u]+path[u][i]) {dist[i]=dist[u]+path[u][i
];
if (!vis[i]) {vis[i]=1;
Q.push (i); }}}}} int main () {#ifdef Online_judge #else freopen ("test.in", "R", stdin); #endif whil
E (~scanf ("%d", &n)) {Rep (i,1,n) {rep (j,1,n) {int C;
scanf ("%d", &path[i][j]);
}} SPFA (1,n);
int ans=dist[n];
int c1=dist[1];
SPFA (N,n);
int c2=dist[n];
printf ("%d\n", Min (ans,c1+c2));
} return 0;}