for(int ik = 1;k <= n;k++){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(dis[i][k] == -1 || dia[k][j] == -1) continue;
if(dis[i][j] == -1 || dis[i][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
void dijkstra(){
//初始化
for(int i = 1;i <= n;i++){
dis[i] = map[i][j];
used[i] = 0;
}
used[1] = 1;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(used[j] == 0 && dis[j] <= min){
min = dis[j];
k = j;
}
}
}
used[k] = 1;
for(int j = 1;j <= n;j++){
if(dis[k] + map[k][j] < dis[j])
dis[j] = dis[k] + map[k][j];
}
}
九度1447:求最短路 题目地址: http://ac.jobdu.com/problem.php?pid=1447
题目描述:
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。输入保证至少存在1条商店到赛场的路线。
当输入为两个0时,输入结束。
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间。
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include
using namespace std;
#define N 101
int dis[N][N];
#define MAX 1000000
int main(){
int n,m;
while(cin >> n >> m){
if(n == 0 &&m == 0) break;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
if(i == j) dis[i][j] = 0;
else dis[i][j] = MAX;
}
for(int i = 0;i < m;i++){
int c1,c2,t;
cin >> c1 >> c2 >> t;
if(t < dis[c1][c2])
dis[c1][c2] = dis[c2][c1] = t;
}
for(int k = 2;k < n;k++){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(dis[i][k] == MAX || dis[k][j] == MAX) continue;
if(dis[i][j] == MAX || dis[i][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
int ans = dis[1][n];
cout << ans << endl;
}
return 0;
}
/**************************************************************
Problem: 1447
User: cherish
Language: C++
Result: Accepted
Time:40 ms
Memory:1560 kb
****************************************************************/
上面这是Floyd解法,下面再给出Dijkstra解法:
#include
using namespace std;
#define N 101
int map[N][N];
int dis[N];
int used[N];
#define MAX 1000000
int main(){
int n,m;
while(cin >> n >> m){
if(n == 0 &&m == 0) break;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
if(i == j) map[i][j] = 0;
else map[i][j] = MAX;
}
for(int i = 0;i < m;i++){
int c1,c2,t;
cin >> c1 >> c2 >> t;
if(t < map[c1][c2])
map[c1][c2] = map[c2][c1] = t;
}
//Dijkstra算法
//初始化 dij_dis存储从节点1到任意节点的距离
for(int i = 1;i <= n;i++) dis[i] = map[1][i];
for(int i = 2;i <= n;i++) used[i] = 0;
used[1] = 1;
for(int i = 1;i <= n;i++){
int min = MAX;
int k;
//遍历所有没有加入集合的节点,找到最短的边
for(int j = 1;j <= n;j++){
if(used[j] == 0 && dis[j] < min){
min = dis[j];
k = j;
}
}
//把最短的边对应的节点加入集合中
used[k] = 1;
//根据新加入的最短路计算源点到其他节点的最短路径
for(int j = 1;j <= n;j++){
if(dis[k] + map[k][j] < dis[j])
dis[j] = dis[k] + map[k][j];
}
}
int ans = dis[n];
cout << ans << endl;
}
return 0;
}
/**************************************************************
Problem: 1447
User: cherish
Language: C++
Result: Accepted
Time:30 ms
Memory:1560 kb
****************************************************************/
3 2 1 2 5 6 2 3 4 5 1 3 0 0
9 11
#include
#include
using namespace std;
#define N 1001
int map[N][N];//邻接矩阵
int cost[N][N];//存储每条边对应的花费
int used[N];//记录是否已经计算出最短路径
int dis[N];//保存源点到任意点的最短距离
int c[N];//保存源点到任意点的最少花费
#define MAX 100000000
int main(){
int n,m;
while(cin >> n >> m){
if(n == 0 && m == 0) break;
//初始化邻接矩阵map和花费cost
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(i == j) {
cost[i][j] = 0;
map[i][j] = 0;
}
else{
cost[i][j] = MAX;
map[i][j] = MAX;
}
}
}
memset(used,0,sizeof(used));
//数据输入
for(int i = 0;i < m;i++){
int v1,v2,dis,c;
cin >> v1 >> v2 >> dis >> c;
map[v1][v2] = map[v2][v1] = dis;
cost[v1][v2] = cost[v2][v2] = c;
}
//输入源点和目的点
int start,end;
cin >> start >> end;
//dijkstra算法初始化 在记录距离dis的同时还要记录花费c
for(int i = 1;i <= n;i++){
c[i] = cost[start][i];
dis[i] = map[start][i];
}
used[start] = 1;//源点记录为已访问
for(int i = 1;i <= n;i++){
int min = MAX;
int k;
for(int j = 1;j <= n;j++){
if(used[j] == 0&&dis[j] <= min){
min = dis[j];
k = j;
}
}
used[k] = 1;
for(int j = 1;j <= n;j++){
if(dis[j] > dis[k] + map[k][j]){
dis[j] = dis[k] + map[k][j];
c[j] = c[k] + cost[k][j];
}
else if(dis[j] == dis[k] + map[k][j]){
if(c[j] > c[k] + cost[k][j]){
c[j] = c[k] + cost[k][j];
}
}
}
}
cout << dis[end] << " " << c[end] << endl;
}
return 0;
}
/**************************************************************
Problem: 1008
User: cherish
Language: C++
Result: Accepted
Time:20 ms
Memory:9360 kb
****************************************************************/
The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.
"For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
Would you please tell Mr. M at least how long will it take to reach his sweet home?
The input contains multiple test cases.
The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
The second line contains one integer M (0<=M<=10000), which is the number of roads.
The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.
For each test case, output one integer representing the minimum time to reach home.
If it is impossible to reach home according to Mr. M's demands, output -1 instead.
2 1 1 2 100 1 2 3 3 1 2 100 1 3 40 2 3 50 1 2 1 5 5 3 1 200 5 3 150 2 5 160 4 3 170 4 2 170 1 2 2 2 1 0
100 90 540
#include
using namespace std;
#define N 610
int map[N][N];
int dis[N];//用来记录从源点到该点的最短路
int leader[N];//记录城市i归属的阵营
int used[N];//记录该城市是否已经计算过最短路径
int main(){
int n,m;
while(cin >> n){
if(n == 0) break;
cin >> m;
//邻接矩阵初始化
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(i == j) map[i][j] = 0;
else map[i][j] = -1;
}
used[i] = 0;
}
for(int i = 0;i < m;i++){
int a,b,t;
cin >> a >> b >> t;
map[a][b] = map[b][a] = t;
}
for(int i = 1;i <= n;i++) cin >> leader[i];
//因为Mr.M不想在两个阵营之间来回穿梭,但是出发城市归属于阵营1而目的城市归属于阵营2
//所以要把所有从阵营2到阵营1的道路设为不可达
for(int i = 1;i <= n;i++){
if(leader[i] == 2){
for(int j = 1;j <= n;j++){
if(leader[j] == 1) map[i][j] = -1;
}
}
dis[i] = map[1][i];//dijsktra算法初始化
}
used[1] = 1;
for(int i =1;i <= n;i++){
int min = 123123123;
int k;
for(int j = 1;j <= n;j++)
if(used[j] == 0 && dis[j] != -1 && dis[j] <= min){
min = dis[j];
k = j;
}
used[k] = 1;
for(int j = 1;j <= n;j++){
if(map[k][j] != -1 && (dis[k] + map[k][j] < dis[j] || dis[j] == -1)){
dis[j] = dis[k] + map[k][j];
}
}
}
cout << dis[2] << endl;
}
return 0;
}
/**************************************************************
Problem: 1162
User: cherish
Language: C++
Result: Accepted
Time:20 ms
Memory:2980 kb
****************************************************************/