POJ3592 Instantaneous Transference tarjan +spfa
链接:http://poj.org/problem?id=3592
题意:题目大意:给定一个矩阵,西南角为出发点,每个单位都有一订价值的金矿(#默示岩石,不成达,*默示时佛门,可以达到指定单位),队#外,每个点只能向右走或向下走,并且可以反复经过一个点。如今请求得最多可以获得多大好处 。
一开始看到这个题,会以为是费用流。但是计划上是在tarjan上的。我觉得如果比赛的时候有这道题估计我也就挂了。
因为这个图大部分的点只有两个后续节点, 但是*会有更多的节点,所以说这样会产生环,因此可以吧整个图求强连通进行缩点,因为每个强连通分量重的点你是可以得到她所有的值。不管走几次,就是那么多,缩完点之后就可以构成DAG这样,就能够求出从belong[1]出发得到的最远的点。一开始逗比了,忘了出发点是左上角,而且dis的赋初值写错了。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <stack>
#define loop(s,i,n) for(i = s;i < n;i++)
#define cl(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = ;
struct edge
{
int u,v,val,next;
}; char map[][];
int dfn[maxn],low[maxn],belong[maxn],inst[maxn];
int head[maxn];
vector<edge>edges,es;
int ihead[maxn];
stack<int> st;
int vis[maxn],dis[maxn];
int dfsclock,scc,cnt;
vector<int>trans;
void init(int n)
{
int i;
loop(,i,n+)
head[i] = -; edges.clear();
es.clear();
dfsclock = scc = cnt = ;
trans.clear();
return ;
}
void addedge(int u,int v,int val)
{
int m;
struct edge e;
e.u = u;
e.v = v;
e.next = head[u];
edges.push_back(e);
m = edges.size();
head[u] = m-;
return ;
}
void tarjan(int i)
{
dfn[i] = low[i] = ++dfsclock;
inst[i] = ;
st.push(i); int j;
edge e;
for(j = head[i]; j != -; j = e.next)
{ e = edges[j];
int v;
v = e.v;
if(!dfn[v])
{
tarjan(v);
low[i] = min(low[v],low[i]);
}
else if(inst[v] && dfn[v] < low[i])
low[i] = dfn[v];
} if(dfn[i] == low[i])
{
scc++;
do
{
j = st.top();
st.pop();
inst[j] = ;
belong[j] = scc;
}
while(i != j);
}
}
void tarjans(int n)
{
dfsclock = scc = ;
memset(dfn,,sizeof(dfn));
while(!st.empty())st.pop(); cl(inst,);
cl(belong,);
int i;
loop(,i,n+)
{
if(!dfn[i]) tarjan(i);
}
return ;
}
int spfa(int s,int n)
{
int i,j;
dis[s] = ;
queue<int>q;
q.push(s);
vis[s] = ;
while(!q.empty())
{
int x;
x = q.front(); q.pop();
vis[x] = ;
struct edge e;
for(i = ihead[x]; i != -; i = e.next)
{
int v; e = es[i];
v = e.v;
if(dis[v] < dis[x]+e.val)
{
dis[v] = dis[x]+e.val;
q.push(v);
vis[v] = ;
}
}
}
int max = ;
for(i = ; i <= n; i++)
{
if(dis[i] > max)
max = dis[i];
} return max;
}
int val[maxn];
int main()
{
int t;
int n,m,k;
scanf("%d",&t);
while(t--)
{
int i,j;
int p;
p = ;
scanf("%d %d",&n,&m);
init(n*m+);
loop(,i,n)
scanf("%s",map[i]);
int leap = ;
for(i = ; i < n; i++)
{
for(j = ; j < m; j++)
{
if(map[i][j] == '*')
trans.push_back(i*m+j+);
}
} loop(,i,trans.size())
{
int l,r;
scanf("%d %d",&l,&r);
if(map[l][r] == '*')
addedge(trans[i],l*m+r+,);
else if(map[l][r] != '#')
addedge(trans[i],l*m+r+,map[l][r]-'');
} loop(,i,n)
{
loop(,j,m)
{
if(map[i][j] != '#')
{ if(i+ < n && map[i+][j] == '*')
addedge(i*m+j+,(+i)*m+j+,);
else if(i+ < n && map[i+][j] != '#')
addedge(i*m+j+,(+i)*m+j+,+map[i+][j]-''); if(j+ < m && map[i][j+] == '*' )
addedge(i*m+j+,(i)*m++j,);
else if( j+ < m && map[i][j+] != '#')
addedge(i*m+j+,i*m+j+,map[i][j+]-'');
}
}
} tarjans(m*n);
// cout<<scc<<endl;
cl(val,); int sum;
sum = ;
cl(vis,);
memset(ihead,-,sizeof(ihead));
for(i = ; i <= m*n; i++)
{
int r,l;
r = (i-)/m;
l = (i-)%m;
if(map[r][l] != '#' && map[r][l] != '*')
val[belong[i]] += map[r][l] - '';
} for(i = ; i < edges.size(); i++)
{
int u,v;
u = edges[i].u;
v = edges[i].v;
if(belong[u] != belong[v])
{
u = belong[u];
v = belong[v];
struct edge e;
e.u = u;
e.v = v;
e.val = val[v];
e.next = ihead[u];
es.push_back(e);
int m;
m = es.size();
ihead[u] = m-;
}
} cl(vis,);
cl(dis,-);
int ans; if(scc == )
printf("%d\n",val[belong[]]);
else
{
ans = ;
int ansi;
ans = spfa(belong[],scc);
printf("%d\n",ans+val[belong[]]);
} }
return ;
}
POJ3592 Instantaneous Transference tarjan +spfa的更多相关文章
- poj3592 Instantaneous Transference tarjan缩点+建图
//给一个n*m的地图.坦克从(0 , 0)開始走 //#表示墙不能走,*表示传送门能够传送到指定地方,能够选择也能够选择不传送 //数字表示该格的矿石数, //坦克从(0,0)開始走.仅仅能往右和往 ...
- POJ3592 Instantaneous Transference 强连通+最长路
题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...
- POJ3592 Instantaneous Transference题解
题意: 给一个矩形,矩形中某些点有一定数量的矿石,有些点为传送点,有些点为障碍.你驾驶采矿车(ore-miner truck,我也不知道是什么),从左上角出发,采尽量多的矿石,矿石不可再生.不能往左边 ...
- poj 3592 Instantaneous Transference 【SCC +缩点 + SPFA】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6204 Accep ...
- bzoj 1179[Apio2009]Atm (tarjan+spfa)
题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...
- 【BZOJ1179】[Apio2009]Atm (tarjan+SPFA)
显而易见的tarjan+spfa...不解释了 ; type edgetype=record toward,next:longint; end; var edge1,edge2:..maxn] of ...
- POJ 3592 Instantaneous Transference(强连通+DP)
POJ 3592 Instantaneous Transference 题目链接 题意:一个图.能往右和下走,然后有*能够传送到一个位置.'#'不能走.走过一个点能够获得该点上面的数字值,问最大能获得 ...
- poj3592Instantaneous Transference(tarjan+spfa)
http://poj.org/problem?id=3592提交了30多次了 受不了了 两份的代码基本上一样了 一个AC一个WA 木办法 贴份别人的吧 改得跟我得一样 人家能A 我是WA.. 强连通 ...
- POJ 3592 Instantaneous Transference(强联通分量 Tarjan)
http://poj.org/problem?id=3592 题意 :给你一个n*m的矩阵,每个位置上都有一个字符,如果是数字代表这个地方有该数量的金矿,如果是*代表这个地方有传送带并且没有金矿,可以 ...
随机推荐
- 【转】Sublime text 3 中文文件名显示方框怎么解决
引用自:http://www.zhihu.com/question/24029280 如图,中文文件名打开全是乱码,内容倒是装了converttoutf8没什么太大的问题. 这个是sublime te ...
- SGU 102
For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprim ...
- 【剑指offer】连续子数组最大和
思路dp很清楚,就是要注意细节. int FindGreatestSumOfSubArray(vector<int> array) { ; ], tempsum = array[]; // ...
- java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片
package com.lp.ecjtu; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...
- ==和equals的区别
== :是判断两个变量或实例是不是指向同一个内存空间equals :是判断两个变量或实例所指向的内存空间的值是不是相同 结论:欲比较栈中数据是否相等,请用= =:欲比较堆中数据是否相等,请用equal ...
- hdu 1404/zoj 2725 Digital Deletions 博弈论
暴力打表!! 代码如下: #include<iostream> #include<algorithm> #include<cstdio> #include<c ...
- Codeforces Round #337 (Div. 2) A. Pasha and Stick 水题
A. Pasha and Stick Pasha has a wooden stick of some positive integer length n. He wants to perform ...
- MVC中前台如何向后台传递数据------$.get(),$post(),$ajax(),$.getJSON()总结
一.引言 MVC中view向controller传递数据的时候真心是一个挺让人头疼的一件事情.因为原理不是很懂只看一写Dome,按葫芦画瓢只能理解三分吧. 二.解读Jquery个Ajax函数 $.ge ...
- linux下修改tomcat的默认目录
1.修改tomcat的默认目录.它的默认目录是webapps/ROOT,对应的conf目录下的server.xml里的内容是: 1.修改tomcat的默认目录.它的默认目录是webapps/ROOT, ...
- iOS 多线程下安全的使用定时器
iOS中timer相关的延时调用,常见的有NSObject中的performSelector:withObject:afterDelay:这个方法在调用的时候会设置当前runloop中timer,还有 ...