POJ 3592 Instantaneous Transference 圖論演算法tarjan+spfa
題意:就是在紅警當中的超時空礦車,它可以傳送到別的地方,求礦車在這個矩形中可以採到的最多的礦產資源。
注意:1、傳送的地方可以選擇,傳送和不傳送;2、就是每次都只能向右或者向下傳遞;
這道題目得建立兩次模型。
第一次:如果一個點是可以到達的(除了#的情況)那麼就建立一條有向邊指向它,表示可以到達,這裡要注意當是*的時候它既可以指向要傳送到的地方又可以到達下一點。這樣就可以建立起來一個有向圖。
然後把有向圖中的強聯通分量進行縮點,得到不同的縮點,再根據縮點之間的聯絡建立起來一個有向圖,再spfa求最長路,就是最大值。
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5367 | Accepted: 1162 |
Description
It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous transfer. When an object uses this magic function, it will be transferred to the specified point immediately, regardless of how far it is.
Now there is a mining area, and you are driving an ore-miner truck. Your mission is to take the maximum ores in the field.
The ore area is a rectangle region which is composed by n × m small squares, some of the squares have numbers of ores, while some do not. The ores can't be regenerated after taken.
The starting position of the ore-miner truck is the northwest corner of the field. It must move to the eastern or southern adjacent square, while it can not move to the northern or western adjacent square. And some squares have magic power that can instantaneously transfer the truck to a certain square specified. However, as the captain of the ore-miner truck, you can decide whether to use this magic power or to stay still. One magic power square will never lose its magic power; you can use the magic power whenever you get there.
Input
The first line of the input is an integer T which indicates the number of test cases.
For each of the test case, the first will be two integers N, M (2 ≤ N, M ≤ 40).
The next N lines will describe the map of the mine field. Each of the N lines will be a string that contains M characters. Each character will be an integer X (0 ≤ X ≤ 9) or a '*' or a '#'. The integer X indicates that square has X units of ores, which your truck could get them all. The '*' indicates this square has a magic power which can transfer truck within an instant. The '#' indicates this square is full of rock and the truck can't move on this square. You can assume that the starting position of the truck will never be a '#' square.
As the map indicates, there are K '*' on the map. Then there follows K lines after the map. The next K lines describe the specified target coordinates for the squares with '*', in the order from north to south then west to east. (the original point is the northwest corner, the coordinate is formatted as north-south, west-east, all from 0 to N - 1,M - 1).
Output
For each test case output the maximum units of ores you can take.
Sample Input
1 2 2 11 1* 0 0
Sample Output
3
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 1000100;
using namespace std;
struct node
{
int u;
int v;
int w;
int next;
} edge1[maxn], edge2[maxn];
stack<int> q;
int low[maxn];//圖中的最小時間戳
int dfn[maxn];//到達這一步的的時候的時間戳
int isb[maxn];
int n, m, nums;
int t, dfs_clock;
int out[maxn];
int num[maxn];
int d[maxn];
int vis[maxn];
int w[maxn];//表示每個強聯通分量中的價值;
int Ore_worth[maxn];
int head1[maxn];
int head2[maxn];
int cnt1;
void init()
{
t = 0;
memset(num, 0 , sizeof(num));
memset(head1, -1, sizeof(head1));
memset(head2, -1, sizeof(head2));
nums = 0;
memset(low, 0 , sizeof(low));
memset(dfn, 0 , sizeof(dfn));
memset(w, 0 , sizeof(w));
dfs_clock = 0;
cnt1 = 0;
}
void add(int u, int v, int w, struct node edge[], int head[])
{
edge[cnt1].u = u;
edge[cnt1].v = v;
edge[cnt1].w = w;
edge[cnt1].next = head[u];
head[u] = cnt1++;
}
void tarjan(int u)
{
low[u] = dfn[u] = ++dfs_clock;
q.push(u);
for(int i = head1[u]; i != -1; i = edge1[i].next)
{
int v = edge1[i].v;
if(!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(!num[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u])
{
++nums;
while(1)
{
int x = q.top();
q.pop();
num[x] = nums;
if(x==u) break;
}
}
}
void spfa(int s)
{
for(int i = 0; i <= n*m; i++)
{
vis[i] = 0;
d[i] = -INF;
}
queue<int>qu;
d[s] = 0;
vis[s] = 1;
qu.push(s);
while(!qu.empty())
{
int u = qu.front();
qu.pop();
vis[u] = 0;
for(int i = head2[u]; i != -1; i = edge2[i].next)
{
int v = edge2[i].v;
int w = edge2[i].w;
if(d[u]+w > d[v])
{
d[v] = d[u]+w;
if(!vis[v])
{
qu.push(v);
vis[v] = 1;
}
}
}
}
}
int main()
{
char str[110][110];
int k;
cin >>k;
while(k--)
{
init();
int i;
cin >>n>>m;
for(i = 0; i < n; i++)
cin >>str[i];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
if(str[i][j] != '#')
{
if(i)
add((i-1)*m+j, i*m+j , 0, edge1, head1);
if(j)
add(i*m+j-1, i*m+j, 0, edge1, head1);
if(str[i][j]=='*')
{
int x, y;
cin>>x>>y;
if(str[x][y]!='#')
add(i*m+j, x*m+y, 0, edge1, head1);
}
}
}
for(i = 0; i < n*m; i++)
if(!dfn[i])
tarjan(i);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(str[i][j]!='*' && str[i][j]!='#')
{
int no = num[i*m+j];
w[no] += str[i][j]-'0';
}
}
}
for(int u = 0; u < n*m; u++)
{
for(int i = head1[u]; i != -1; i = edge1[i].next)
{
int v = edge1[i].v;
if(num[u] != num[v])
{
add(num[u], num[v], w[num[v]], edge2, head2);
}
}
}
spfa(num[0]);
int _max = -1;
for(i = 1; i <= nums; i++)
if(d[i] > _max)
_max = d[i];
cout<<_max+w[num[0]]<<endl;
}
return 0;
}
相關文章
- POJ 1611 The Suspects 圖論圖論
- 【轉載】POJ 圖論題目圖論
- 圖論演算法圖論演算法
- 【圖論】Floyd演算法圖論演算法
- BZOJ3592 : Architext
- POJ 基本演算法演算法
- 圖論與圖學習(二):圖演算法圖論演算法
- 圖論系列之「讀取圖演算法」圖論演算法
- 演算法-圖論-拓撲排序演算法圖論排序
- 「圖論」Bron-kerbosch演算法圖論演算法
- POJ 1325-Machine Schedule(二分圖匹配-匈牙利演算法)Mac演算法
- poj2400 KM演算法二分圖的完美匹配演算法
- 圖論-二分圖匹配匈牙利演算法圖論演算法
- 【JAVA演算法】圖論演算法 -- Dijkstra演算法Java演算法圖論
- 圖論演算法遍歷基礎圖論演算法
- 演算法-圖論-最小生成樹演算法圖論
- 圖論最短路演算法筆記圖論演算法筆記
- POJ 1523 SPF (無向圖求割點 tarjan演算法)演算法
- 圖論(三)--各種基礎圖演算法總結圖論演算法
- POJ 1469-COURSES(二分圖匹配入門-匈牙利演算法)演算法
- 圖論——強連通分量(Tarjan演算法)圖論演算法
- 【知識】圖論 朱劉演算法梳理圖論演算法
- 圖論——環測定與矩陣演算法圖論矩陣演算法
- 《圖論》——深度優先搜尋演算法(DFS)圖論演算法
- 演算法隨筆——圖論之差分約束演算法圖論
- 圖論中的最小生成樹演算法圖論演算法
- 【JAVA演算法】圖論演算法 --求最小生成樹Prim演算法Java演算法圖論
- 《圖論》——廣度優先遍歷演算法(BFS)圖論演算法
- 演算法資料結構 | 圖論基礎演算法——拓撲排序演算法資料結構圖論排序
- poj 3164 Command Network(最小樹形圖模板題)朱_ 劉演算法演算法
- 圖論圖論
- 圖論演算法 有圖有程式碼 萬字總結 向前輩致敬圖論演算法
- 圖論演算法 有圖有程式碼 萬字總結 向前輩致敬圖論演算法
- 《圖論》——最短路徑 Dijkstra演算法(戴克斯特拉演算法)圖論演算法
- poj 3436 最大流的增廣路演算法演算法
- 十個基礎圖論演算法介紹 - Franc0圖論演算法
- 圖論中的常見演算法分析比較和模板圖論演算法
- 程式碼隨想錄演算法訓練營第64天 | 圖論:Floyd 演算法+A * 演算法演算法圖論