最短路:在某些最短路的问题中,可能会被正向建的图卡时间,然而我们反向建图后,可能会过掉卡你的那一组数据,所以有时候我们建一个反向图可能会起到出其不意的效果!
此题的正解应该是:拓扑排序+dp求一个最长路,当然我们很容易想到用spfa来求一遍最长路,同时,此题没有环,我们还可以用dijstra来求,但是因为图中有负边,所以我们不能用vis来剪枝,这样直接跑出来的结果都会是对的,而且卡时间卡的贼刺激!
接下来贴一下几种代码!
topo+dp(2047MS)
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1010;
const double pi = 3.1415926;
using namespace std;
int n, m, cnt;
LL dp[100010];
int head[100010];
int in[100010], out[100010], a[100010];
struct edge
{
int to;
int next;
}e[1000010];
void add(int u, int v)
{
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt ++;
}
void topo()
{
queue<int> q;
for(int i = 1; i <= n; i ++) {
if(in[i] == 0) {
q.push(i);
dp[i] = a[i];
}
}
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
int en = e[i].to;
in[en] --;
if(in[en] == 0) q.push(en);
dp[en] = max(dp[en], dp[u] + a[en]);
}
}
LL ans = -1e18;
for(int i = 1; i <= n; i ++) {
if(out[i] == 0) ans = max(ans, dp[i]);
}
printf("%lld\n", ans);
}
void inti()
{
mem1(head);
mem0(in);
mem0(out);
cnt = 0;
for(int i = 0; i <= n; i ++) {
dp[i] = -1e18;
}
}
int main(int argc, char * argv[])
{
int x, y, z;
while(~scanf("%d%d", &n, &m)) {
inti();
for(int i = 1; i <= n; i ++){
scanf("%d",&a[i] );
}
for(int i = 0; i < m; i ++) {
scanf("%d%d", &x, &y);
add(x, y);
in[y] ++;
out[x] ++;
}
topo();
}
}
SPFA(3797MS)贴一份别人的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define N 100001 //顶点数
#define M 1000001 //边数
//poj3249 DAG 最长路?
const LL INF = 1LL<<60;
struct edge {
int y, next;
};
int V, E; //顶点数,边数
LL d[N]; //最长距离
int q[M], head, tail; //队列,头,尾
int done[N]; //是否在队列里
edge e[M]; //E条边
int next[N], el; //i的下一条边的位置,记录边数
int val[N]; //节点值
int cnti[N]; //是否有入度
void add(int x, int y) { //加边
e[++el].y = y;
e[el].next = next[x];
next[x] = el;
}
void spfa() {
head = tail = 0;
rep1(i,V) d[i] = -INF, done[i] = 0, q[i] = 0;
rep1(s,V) if(cnti[s]==0) {
d[s] = val[s];
done[s] = 1;
q[tail++] = s;
}
while(head < tail) {
int x = q[head++];
done[x] = 0;
for(int i=next[x]; i; i=e[i].next) {
int y = e[i].y;
LL tmp = d[x]+(LL)val[y];
if(d[y] < tmp) { //松弛操作
d[y] = tmp;
if(!done[y]) { //不在队列,入队
q[tail++] = y;
done[y] = 1;
}
}
}
}
}
int main() {
while(scanf("%d%d", &V,&E) != EOF) {
el = 0;
rep1(i,V) next[i] = cnti[i] = 0;
rep1(i,V) scanf("%d", val+i);
for(int i=0, x,y; i<E; i++) {
scanf("%d%d", &x,&y);
add(y,x); //反向建图
cnti[x]=1;
}
spfa();
LL ans = -INF;
rep1(i,V) {
if(next[i]==0 && ans < d[i]) {
ans = d[i];
}
}
printf("%I64d\n", ans);
}
return 0;
}
dijstra(4875MS) 这个最刺激,去掉vis剪枝竟然卡过去了
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1010;
const double pi = 3.1415926;
using namespace std;
int n, m, cnt;
LL d[100010], ans;
int head[100010];
int in[100010], out[100010], a[100010];
int vis[100010];
struct edge
{
int to;
int next;
}e[1000010];
void add(int u, int v)
{
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt ++;
}
void dijstra()
{
priority_queue<LLpar> q;
for(int i = 1; i <= n; i ++) {
if(in[i] == 0) {
q.push(make_pair(a[i], i));
d[i] = a[i];
}
}
while(!q.empty()) {
LLpar cur = q.top();
q.pop();
int u = cur.second;
LL w = cur.first;
//if(vis[u]) continue; 有负边,这个剪枝不能用
//vis[u] = 1;
for(int i = head[u]; i != -1; i = e[i].next) {
int en = e[i].to;
LL len = a[en] + w;
if(d[en] < len) {
d[en] = len;
q.push(make_pair(len, en));
}
}
}
for(int i = 1; i <= n; i ++) {
if(out[i] == 0) ans = max(ans, d[i]);
}
printf("%lld\n", ans);
}
void inti()
{
mem1(head);
mem0(in);
mem0(out);
mem0(vis);
cnt = 0;
for(int i = 0; i <= n; i ++) {
d[i] = -1e18;
}
}
int main(int argc, char * argv[])
{
int x, y, z;
while(~scanf("%d%d", &n, &m)) {
inti();
for(int i = 1; i <= n; i ++){
scanf("%d",&a[i] );
}
for(int i = 0; i < m; i ++) {
scanf("%d%d", &x, &y);
add(y, x);
in[x] ++;
out[y] ++;
}
ans = -1e18; //这个初始化也有点坑!
dijstra();
}
}