大意:给你一个无向图,让你求到达终点的最短距离然后再返回求最短距离,但是不能经过同一条边两次。
思路:我的思路是去的时候求一次最短路,回来是将所有边反向,然后求一次最短路,测试数据过了,如果是有向图的话,应该可以AC,无向图不知为啥WA,欢迎大牛们指点。
等我学了最小费用流的时候再来看看。12.10.25
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 1000001
#define INF 0x3f3f3f3f
struct Edge
{
int u, v, next;
int w;
}edge[4*MAXN], edge2[4*MAXN];
int first[MAXN], first2[MAXN];
int d[MAXN];
int n, m, cnt;
void init()
{
cnt = 0;
memset(first, -1, sizeof(first));
memset(first2, -1, sizeof(first));
}
void spfa(int src)
{
queue<int> q;
bool inq[MAXN] = {0};
for(int i = 1; i <= n; i++) d[i] = (i == src)? 0:INF;
q.push(src);
while(!q.empty())
{
int x = q.front(); q.pop();
inq[x] = 0;
for(int e = first[x]; e != -1; e = edge[e].next)
{
int v = edge[e].v, w = edge[e].w;
if(d[v] > w + d[x])
{
d[v] = d[x] + w;
if(!inq[v])
{
inq[v] = 1;
q.push(v);
}
}
}
}
}
void spfa2(int src)
{
queue<int> q;
bool inq[MAXN] = {0};
for(int i = 1; i <= n; i++) d[i] = (i == src)? 0:INF;
q.push(src);
while(!q.empty())
{
int x = q.front(); q.pop();
inq[x] = 0;
for(int e = first2[x]; e != -1; e = edge2[e].next)
{
int v = edge2[e].v, w = edge2[e].w;
if(d[v] > d[x] + w)
{
d[v] = d[x] + w;
if(!inq[v])
{
inq[v] = 1;
q.push(v);
}
}
}
}
}
void read_graph(int u, int v, int w) //正反向图
{
edge[cnt].v = v, edge[cnt].w = w;
edge[cnt].next = first[u], first[u] = cnt;
edge2[cnt].v = u, edge2[cnt].w = w;
edge2[cnt].next = first2[v], first2[v] = cnt++;
}
void solve()
{
int ans = 0;
spfa(1);
ans += d[n];
if(ans >= INF)
{
printf("Back to jail\n");
return ;
}
spfa2(1);
ans += d[n];
if(ans >= INF)
{
printf("Back to jail\n");
}
else
{
printf("%d\n", ans);
}
}
int main()
{
while(scanf("%d%d", &n, &m))
{
init();
while(m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
read_graph(u, v, w);
}
solve();
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 1000001
#define INF 0x3f3f3f3f
struct Edge
{
int u, v, next;
int w;
}edge[4*MAXN], edge2[4*MAXN];
int first[MAXN], first2[MAXN];
int d[MAXN];
int n, m, cnt;
void init()
{
cnt = 0;
memset(first, -1, sizeof(first));
memset(first2, -1, sizeof(first));
}
void spfa(int src)
{
queue<int> q;
bool inq[MAXN] = {0};
for(int i = 1; i <= n; i++) d[i] = (i == src)? 0:INF;
q.push(src);
while(!q.empty())
{
int x = q.front(); q.pop();
inq[x] = 0;
for(int e = first[x]; e != -1; e = edge[e].next)
{
int v = edge[e].v, w = edge[e].w;
if(d[v] > w + d[x])
{
d[v] = d[x] + w;
if(!inq[v])
{
inq[v] = 1;
q.push(v);
}
}
}
}
}
void spfa2(int src)
{
queue<int> q;
bool inq[MAXN] = {0};
for(int i = 1; i <= n; i++) d[i] = (i == src)? 0:INF;
q.push(src);
while(!q.empty())
{
int x = q.front(); q.pop();
inq[x] = 0;
for(int e = first2[x]; e != -1; e = edge2[e].next)
{
int v = edge2[e].v, w = edge2[e].w;
if(d[v] > d[x] + w)
{
d[v] = d[x] + w;
if(!inq[v])
{
inq[v] = 1;
q.push(v);
}
}
}
}
}
void read_graph(int u, int v, int w) //正反向图
{
edge[cnt].v = v, edge[cnt].w = w;
edge[cnt].next = first[u], first[u] = cnt;
edge2[cnt].v = u, edge2[cnt].w = w;
edge2[cnt].next = first2[v], first2[v] = cnt++;
}
void solve()
{
int ans = 0;
spfa(1);
ans += d[n];
if(ans >= INF)
{
printf("Back to jail\n");
return ;
}
spfa2(1);
ans += d[n];
if(ans >= INF)
{
printf("Back to jail\n");
}
else
{
printf("%d\n", ans);
}
}
int main()
{
while(scanf("%d%d", &n, &m))
{
init();
while(m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
read_graph(u, v, w);
}
solve();
}
return 0;
}