B - 我想追小姐姐了!
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?
Input
输入包括多组数据。每组数据第一行是两个整数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条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
解题:
一个最短路径的普及题目,题目所描述的图只有一个 时间权值 ,使用 邻接矩阵 和 Bellman-Ford算法 就行了,不过Bellman-Ford算法适用于有向图,在解题时在此算法上面改了一点点,使之能够适用于无向图了。
需要注意的是,两个点之间可能不止一条路,我们只取最短的那一条就行了。
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxe = 100100, maxv = 1100;
const int INF = unsigned(-1) >> 1;
int n, m, s, t;
struct edge
{
int x, y;
};
edge es[maxe];
struct Graph
{
int dis;
};
Graph G[maxv][maxv];
struct dist
{
int dis;
};
dist d[maxv];
void fun(int s)
{
for (int i = 1; i <= n; i++)
d[i].dis = INF;
d[s].dis = 0;
while (true)
{
bool updata = false;
for (int i = 0; i < m; i++) //遍历所有的边
{
edge e = es[i];
if (d[e.x].dis != INF) //如果遍历过e.x点
{
if (d[e.x].dis + G[e.x][e.y].dis < d[e.y].dis) //如果从e.x点走到e.y点的路径 比 其他点走到e.y的路径要短
{
d[e.y].dis = d[e.x].dis + G[e.x][e.y].dis; //更新e.y的路径为更短的
updata = true;
}
}
if (d[e.y].dis != INF) //如果遍历过e.y点
{
if (d[e.y].dis + G[e.x][e.y].dis < d[e.x].dis) //如果从e.y点走到e.x点的路径 比 其他点走到e.x的路径要短
{
d[e.x].dis = d[e.y].dis + G[e.x][e.y].dis; //更新e.x的路径为更短的
updata = true;
}
}
}
if (updata == false) //如果所有的边都遍历过了,路径信息没有更新过,说明已经遍历完了,结束搜索
break;
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
int dis;
if (n == 0 && m == 0)
break;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
G[i][j].dis = INF;
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &es[i].x, &es[i].y, &dis);
if (dis < G[es[i].x][es[i].y].dis)
G[es[i].x][es[i].y].dis = G[es[i].y][es[i].x].dis = dis;
}
fun(1);
printf("%d\n", d[n].dis);
}
return 0;
}
A - 炎热的8月
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output
9 11
解题
这是一个带有 两个权值 的最短路径问题,在求解的时候注意 两个权值的优先级 , 即距离相同时,取花费小的路径 。
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxe = 100100, maxv = 1100;
const int INF = unsigned(-1) >> 1;
int n, m, s, t;
struct edge
{
int x, y;
};
edge es[maxe];
struct Graph
{
int dis, cost;
};
Graph G[maxv][maxv];
struct dist
{
int dis, cost;
};
dist d[maxv];
void fun(int s)
{
for (int i = 1; i <= n; i++)
d[i].dis = d[i].cost = INF;
d[s].dis = d[s].cost = 0;
while (true)
{
bool updata = false;
for (int i = 0; i < m; i++) //遍历所有的边
{
edge e = es[i];
// printf("%d:%d %d ->%d:%d %d\n", e.x, d[e.x].dis, d[e.x].cost, e.y, d[e.x].dis + G[e.x][e.y].dis, d[e.x].cost + G[e.x][e.y].cost);
if (d[e.x].dis != INF) //如果遍历过e.x点
{
if (d[e.x].dis + G[e.x][e.y].dis < d[e.y].dis) //如果从e.x点走到e.y点的路径 比 其他点走到e.y的路径要短
{
d[e.y].dis = d[e.x].dis + G[e.x][e.y].dis; //更新e.y的路径为更短的
d[e.y].cost = d[e.x].cost + G[e.x][e.y].cost; //花费也要更新
// printf("%d->%d: %d %d\n", e.x, e.y, d[e.y].dis, d[e.y].cost);
updata = true;
}
else if (d[e.x].dis + G[e.x][e.y].dis == d[e.y].dis) //如果从e.x点走到e.y点的路径 和 其他点走到e.y的路径一样长
{
if (d[e.x].cost + G[e.x][e.y].cost < d[e.y].cost) //就取花费最小的路
{
d[e.y].cost = d[e.x].cost + G[e.x][e.y].cost;
// printf("%d->%d: %d %d\n", e.x, e.y, d[e.y].dis, d[e.y].cost);
updata = true;
}
}
}
if (d[e.y].dis != INF) //如果遍历过e.y点
{
if (d[e.y].dis + G[e.x][e.y].dis < d[e.x].dis) //如果从e.y点走到e.x点的路径 比 其他点走到e.x的路径要短
{
d[e.x].dis = d[e.y].dis + G[e.x][e.y].dis; //更新e.x的路径为更短的
d[e.x].cost = d[e.y].cost + G[e.x][e.y].cost; //花费也要更新
// printf("%d->%d: %d %d\n", e.y, e.x, d[e.x].dis, d[e.x].cost);
updata = true;
}
else if (d[e.y].dis + G[e.x][e.y].dis == d[e.x].dis) //如果从e.y点走到e.x点的路径 和 其他点走到e.x的路径一样长
{
if (d[e.y].cost + G[e.x][e.y].cost < d[e.x].cost) //就取花费最小的路
{
d[e.x].cost = d[e.y].cost + G[e.x][e.y].cost;
// printf("%d->%d: %d %d\n", e.y, e.x, d[e.x].dis, d[e.x].cost);
updata = true;
}
}
}
}
if (updata == false) //如果所有的边都遍历过了,路径信息没有更新过,说明已经遍历完了,结束搜索
break;
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
int dis, cost;
if (n == 0 && m == 0)
break;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
G[i][j].dis = G[i][j].cost = INF;
for (int i = 0; i < m; i++)
{
scanf("%d%d%d%d", &es[i].x, &es[i].y, &dis, &cost);
if (dis < G[es[i].x][es[i].y].dis)
{
G[es[i].x][es[i].y].dis = G[es[i].y][es[i].x].dis = dis;
G[es[i].x][es[i].y].cost = G[es[i].y][es[i].x].cost = cost;
}
else if (dis == G[es[i].x][es[i].y].dis && cost < G[es[i].x][es[i].y].cost)
{
G[es[i].x][es[i].y].cost = G[es[i].y][es[i].x].cost = cost;
}
}
scanf("%d%d", &s, &t);
fun(s);
printf("%d %d\n", d[t].dis, d[t].cost);
}
return 0;
}