Topic Links:
option=com_onlinejudge&itemid=8&page=show_problem&problem=1544 ">click here~
I expected to strengthen the data, in my struggle for a very long time I handed over several online code is not WA or tle. When I was very confused, I made another one, AC (although I used random data to find the data he can't get through the code).
Gave me the confidence. Then I took his code and tested it with my code in random numbers. Then use FC to find different. A fatal error was found. Generally, BFS or DFS are required to have a vis array or hash, but this problem is very big problem, the same for 3 Water Cup state, in 3 steps may flow is 50, then this state is marked, 5 steps of the time again appeared this state (can be understood as there is a branch), But the flow is 35, assuming a two-dimensional array of weight (no need to use three-dimensional, because and is the same, two-dimensional can be), then the state of the traffic will not be updated. The problem is to use the smallest amount of traffic to reach this state, whether it's getting a goal or a short-term goal.
So I feel that the standard solution to this problem is not BFS, because it can be said to be a violent. Each time the state (assuming less traffic than the previous to this state) needs to be updated, constantly updated until the end.
The idea for this problem on UVA Toolkit is DP or Dijkstra. These 2 things are not very good. No idea was built. I feel like i built it or searched for something.
Although a, but feel uncomfortable ah. In addition, the code I found for the A has not been data: 33 12 113 6
His conclusion is 174 6, and my conclusion is 171 6, which is also my answer on UVA toolkit. But I think his train of thought should be correct, did not look closely. It's too long.
Imagine a welcome message with an idea.
Code for reference:
#include <cstdio> #include <ctype.h> #include <algorithm> #include <iostream> #include < cstring> #include <vector> #include <stack> #include <cmath> #include <queue> #include < set>using namespace std; #define LL long long#define NMAX 20000typedef int state[3];state st[nmax],a;int pour[nmax];int Record[205];int target;int vis[205][205],vispour[205][205];int try_to_insert (int x,int tpour) {state &k = st[x]; if (vis[k[0]][k[1]]! = 1 | | vispour[k[0]][k[1]] > Tpour) {vis[k[0]][k[1]] = 1; VISPOUR[K[0]][K[1]] = Tpour; return 1; } return 0;} int main () {//Freopen ("Input.txt", "R", stdin);//Freopen ("O.txt", "w", stdout); int I,j,t,ans; scanf ("%d", &t); while (t--) {memset (record,0,sizeof (record)); memset (vis,0,sizeof (VIS)); memset (vispour,0,sizeof (VIS)); scanf ("%d%d%d%d", &a[0],&a[1],&a[2],&target); St[1][0] = st[1][1] = 0; ST[1][2] = a[2]; memset (pour,0,sizeof (pour)); RECORD[A[2]] = 1; int front = 1,rear = 2; Vis[0][0] = 1; BOOL flag = FALSE; while (front < rear) {for (i = 0; i < 3; i++) if (record[st[front][i]] = = 0 | | pour[r Ecord[st[front][i]] > Pour[front]) record[st[front][i] = front; for (i = 0; i < 3; i++) if (st[front][i] = = target) {if (flag) ans = Pour[ans] > pour[front]?front:ans; else ans = front; Flag = true; Break } for (i = 0; i < 3; i++) {state &w = St[front]; if (w[i]! = 0) {for (j = 0; J < 3; J + +) {if (i = = J | | (I! = J && w[j] = = A[j])) Continue State&temp = St[rear]; memcpy (Temp,w,sizeof (w)); int pp; if (W[i] + w[j] > a[j]) {pp = a[j]-w[j]; Temp[i] = w[i]-pp; TEMP[J] = A[j]; } else {pp = w[i]; Temp[i] = 0; TEMP[J] = w[j] + pp; } int tpour; Tpour = Pour[front] + pp; if (Try_to_insert (Rear,tpour)) {pour[rear] = Tpour; rear++; }}}} front++; } if (record[target] = = 0) {for (i = target; record[i] = = 0; i--); printf"%d%d\n", pour[record[i]],i); } else printf ("%d%d\n", pour[ans],target); } return 0;}
UVA 10603 Fill (the correct code, although very rubbing, many of the online code is not AC)