TOJ 3010 Reverse a Road / dijkstra

Reverse a Road

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte
 

描述

Peter resides in the city of Nanuh, and goes to his working place in this city every weekday.He has been totally annoyed with the road traffic of this city. All the roads in this city are one-way, so he has to drive a longer way than he thinks he need.
One day, the following thought has come up to Peter’s mind: “How about making the sign of one road indicate the opposite direction? I think my act won't be out as long as I change just one sign. Well, of course I want to make my route to the working place shorter as much as possible. Which road should I alter the direction of?” What a clever guy he is.
You are asked by Peter to write a program that finds the shortest route when the direction of up to one road is allowed to be altered. You don’t have to worry about the penalty for complicity, because you resides in a different country from Peter and cannot be punished by the law of his country. So just help him!

输入

The input consists of a series of datasets, each of which is formatted as follows:

N    
S T  
M    
A1 B1
A2 B2
: : :
AM BM

N denotes the number of points. S and T indicate the points where Andrew’s home and working place are located respectively. M denotes the number of roads. Finally, Ai and Bi indicate the starting and ending points of the i-th road respectively. Each point is identified by a unique number from 1 to N. Some roads may start and end at the same point. Also, there may be more than one road connecting the same pair of starting and ending points.
You may assume all the following: 1 ≤ N ≤ 1000, 1 ≤ M ≤ 10000, and S, T.

输出

For each dataset, print a line that contains the shortest distance (counted by the number of passed roads) and the road number whose direction should be altered. If there are multiple ways to obtain the shortest distance, choose one with the smallest road number. If no direction change results in a shorter route, print 0 as the road number.
Separate the distance and the road number by a single space. No extra characters are allowed.

样例输入

4
1 4
4
1 2
2 3
3 4

4 1

样例输出

1 4

求一遍起点到个点的最短路

在反向求一次终点到个点的最短路

在枚举每一条边

以前做过类似的题目 上次是无向图 这次是有向图

#include <stdio.h>
#include <string.h>
#define inf 999999999 
int a[2][1010][1010];
int dis[2][1010];
int vis[1010];
struct node
{
	int s;
	int e;
}v[10010];
int n,m;
int s,e;

void dijkstra(int flag,int x)
{
	int i,j;
	for(i = 1; i <= n; i++)
	{
	//	if(a[flag][x][i] != -1)
	//		dis[flag][i] = a[flag][x][i];
	//	else
		dis[flag][i] = a[flag][x][i];
		vis[i] = 0;
	}
	dis[flag][x] = 0;
	vis[x] = 1;
	for(i = 1;i < n; i++)
	{
		int min = inf, k = 0;
		for(j = 1; j <= n; j++)  
		{
			if(!vis[j] && dis[flag][j] < min)
			{
				min = dis[flag][j];
				k = j;	
			}
		}
		if(k == 0)
			break;
		vis[k] = 1;
		for(j = 1;j <= n; j++) 
		{
			if(!vis[j] && dis[flag][j] > dis[flag][k] + a[flag][k][j])  
    			dis[flag][j] = dis[flag][k] + a[flag][k][j];
		}
	}
}
int main()
{
	int i,j,k,x,y;
	while(scanf("%d",&n)!=EOF)
	{
		for(k = 0;k < 2; k++)
		{
			for(i = 1;i <= n; i++)
			{
				for(j = 1;j <= n; j++)
				{
					if(i == j)
						a[k][i][j] = 0;
					else
						a[k][i][j] = inf;
				}
			}
		}
		scanf("%d %d",&s,&e);
		scanf("%d",&m);
		for(i = 0;i < m; i++)
		{
			scanf("%d %d",&v[i].s,&v[i].e);
			a[0][v[i].s][v[i].e] = 1;
			a[1][v[i].e][v[i].s] = 1;
		}
		dijkstra(0,s);
		dijkstra(1,e);
		int min = dis[0][e];
		int k = 0;
		for(i = 0;i < m; i++)
		{
			if(min > dis[1][v[i].s] + dis[0][v[i].e] + 1)
			{
				min = dis[1][v[i].s] + dis[0][v[i].e] + 1;
				k = i + 1;
			}
		}
		printf("%d %d\n",min,k);
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值