poj 3268 Silver Cow Party & zoj 2008 Invitation Cards

参考 算法导论 p580 单终点最短路径问题

这两题是一样的求法,先正着求一次最短路,再将边反向,再求一次

矩阵表示图的话就是将矩阵转置一次,邻接表表示的话就是读入边的时候,建两个表

poj3268

dijkstra求最短路

  
    
#include < iostream >
#include
< string .h >
using namespace std;
const int MAXN = 1001 ;
int n,m,x;
const int INF = 0x7FFF ;
int G[MAXN][MAXN];
int dist1[MAXN];
int sum[MAXN];
bool used[MAXN];

void in (){
int i,j,from,to,t;
cin
>> n >> m >> x;
for (i = 1 ; i <= n;i ++ ){
for (j = 1 ; j <= n; j ++ ){
if (i != j){
G[i][j]
= INF;
}
else {
G[i][j]
= 0 ;
}
}
}
for (i = 1 ; i <= m;i ++ ){
cin
>> from >> to >> t;
G[from][to]
= t;
}
memset(sum,
0 , sizeof (sum));
}

void dijkstra(){
int i,j;
for (i = 1 ; i <= n;i ++ ){
dist1[i]
= G[x][i];
used[i]
= false ;
}
used[x]
= true ;
dist1[x]
= 0 ;
for (i = 1 ; i < n; i ++ ){
int min = INF,minIndex = x;
for (j = 1 ; j <= n; j ++ )
if ( ! used[j] && dist1[j] < min){
min
= dist1[j];
minIndex
= j;
}
used[minIndex]
= true ;
sum[minIndex]
+= dist1[minIndex];
for (j = 1 ; j <= n; j ++ ){
if ( ! used[j] && dist1[j] > dist1[minIndex] + G[minIndex][j]){
dist1[j]
= dist1[minIndex] + G[minIndex][j];
}
}
}
}

void reverse(){
int i,j,temp;
for (i = 1 ; i <= n; i ++ ){
for (j = 1 ; j < i; j ++ ){
temp
= G[i][j];
G[i][j]
= G[j][i];
G[j][i]
= temp;
}
}
}

int main(){
int max = 0 ,i;
in ();
dijkstra();
reverse();
dijkstra();
for (i = 1 ; i <= n; i ++ )
if (max < sum[i])
max
= sum[i];
cout
<< max << endl;
return 0 ;
}

zoj2008

spfa 求最短路

  
    
#include < stdio.h >
#include
< queue >
using namespace std;
const int MAXN = 1000000 ;
const int INF = 0x3f3f3f3f ;
struct edge{
int u, v, w, next, next2;
}e[MAXN];
int first[MAXN], first2[MAXN], dist[MAXN];
queue
< int > que;
int p, q, cnt;
bool inq[MAXN];
void spfa(){
dist[
1 ] = 0 ;
inq[
1 ] = true ;
while ( ! que.empty())
que.pop();
que.push(
1 );
while ( ! que.empty()){
int x = que.front();
que.pop();
inq[x]
= false ;
for ( int i = first[x]; i != - 1 ; i = e[i].next){
int u = e[i].u, v = e[i].v, w = e[i].w;
if (dist[v] > dist[u] + w){
dist[v]
= dist[u] + w;
if ( ! inq[v]){
inq[v]
= true ;
que.push(v);
}
}
}
}
}
void spfa2(){
for ( int i = 1 ; i <= p; ++ i){
inq[i]
= false ;
dist[i]
= INF;
}
dist[
1 ] = 0 ;
inq[
1 ] = true ;
while ( ! que.empty())
que.pop();
que.push(
1 );
while ( ! que.empty()){
int x = que.front();
que.pop();
inq[x]
= false ;
for ( int i = first2[x]; i != - 1 ; i = e[i].next2){
int u = e[i].v, v = e[i].u, w = e[i].w;
if (dist[v] > dist[u] + w){
dist[v]
= dist[u] + w;
if ( ! inq[v]){
inq[v]
= true ;
que.push(v);
}
}
}
}
}
int main(){
int t;
scanf(
" %d " , & t);
while (t -- ){
scanf(
" %d%d " , & p, & q);
for ( int i = 1 ; i <= p; ++ i){
first[i]
= - 1 ;
first2[i]
= - 1 ;
dist[i]
= INF;
inq[i]
= false ;
}
for ( int i = 0 ; i < q; ++ i){
scanf(
" %d%d%d " , & e[i].u, & e[i].v, & e[i].w);
e[i].next
= first[e[i].u];
first[e[i].u]
= i;
e[i].next2
= first2[e[i].v]; // 建立两个邻接表
first2[e[i].v] = i;
}
spfa();
int ans = 0 ;
for ( int i = 1 ; i <= p; ++ i)
ans
+= dist[i];
spfa2();
for ( int i = 1 ; i <= p; ++ i)
ans
+= dist[i];
printf(
" %d\n " ,ans);
}
return 0 ;
}

你可能感兴趣的:(2008)