杭电ACM 3790:最短路径问题C++解法的代码
作者:互联网
在内容过程中,将写内容过程中重要的内容收藏起来,下面的内容内容是关于杭电ACM 3790:最短路径问题C++解法的内容,希望能对大伙有些好处。
#include
#include
#include
using namespace std;
#define MAX 1002
#define inf 999999
int map[MAX][MAX],cost[MAX][MAX];
int n;
{
int i,j,MIN,v;
int flag[MAX],dis[MAX],value[MAX];
for(i=1;i<=n;i++)
{
dis[i]=map[st][i];
}
memset(flag,0,sizeof(flag));
flag[st]=1;
for(i=1;i<=n;i++)
{
MIN=inf;
for(j=1;j<=n;j++)
{
if(!flag[j]&&MIN>dis[j])
{
v=j;
MIN=dis[j];
}
}
if(MIN==inf)break;
flag[v]=1;
for(j=1;j<=n;j++)
{
if(!flag[j]&&map[v][j]<inf)
{
if(dis[j]>dis[v]+map[v][j])
{
dis[j]=dis[v]+map[v][j];
}
else
{
if(value[j]>value[v]+cost[v][j])
value[j]=value[v]+cost[v][j];
}
}
}
}
}
int main()
{
int i,j,m,a,b,d,p,st,en;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n0&&m0)break;
for(i=1;i<=n;i++)
{
map[i][j]=inf;
cost[i][j]=inf;
}
while(m–)
{
scanf("%d%d%d%d",&a,&b,&d,&p);
if(d<map[a][b]||(d==map[a][b]&&p<cost[a][b]))
{
map[a][b]=map[b][a]=d;
}
}
scanf("%d%d",&st,&en);
DJ(st,en);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX 1001
#define INF 999999999
typedef struct _road
{
int d;
int p;
}road;
road map[MAX][MAX];
int n, m;
void init()
{
int i, j;
for(i = 1; i <= n; i ++){
for(j = 1; j <= n; j ++){
map[i][j].d = INF;
map[i][j].p = INF;
}
}
}
void dijkstra(int start, int end)
{
int dist[MAX];
int cost[MAX];
int min1, min2;
int pre[MAX];
memset(pre, 0, sizeof(pre));
int i, j, v;
for(i = 1; i <= n; i ++){
dist[i] = map[start][i].d;
cost[i] = map[start][i].p;
}
for(i = 1; i < n; i ++){
min1 = INF;
for(j = 1; j <= n; j ++){
if(pre[j] == 0 && (min1 > dist[j] || (dist[j] == min1 && min2 > cost[j]))){
v = j;
min1 = dist[j];
min2 = cost[j];
}
}
if(min1 == INF)break;
for(j = 1; j <= n; j ++){
if(pre[j] == 0){
if((dist[v] + map[v][j].d) <= dist[j] || (dist[v] + map[v][j].d == dist[j] && cost[v] + map[v][j].p < cost[j])){
dist[j] = dist[v] + map[v][j].d;
cost[j] = cost[v] + map[v][j].p;
}
}
}
}
printf("%d %dn", dist[end], cost[end]);
}
int main()
{
int a, b, d, p;
int i;
int s, t;
while(scanf("%d%d", &n, &m)!=EOF){
if(n ==0 && m == 0)break;
init();
for(i = 0; i < m; i ++){
if(d < map[a][b].d || (d == map[a][b].d && p < map[a][b].p)){
map[a][b].d = map[b][a].d = d;
map[a][b].p = map[b][a].p = p;
}
}
scanf("%d%d", &s, &t);
dijkstra(s, t);
}
return 0;
}
#include<stdio.h>
#define FINITY 0x7fffffff
#define M 1005
typedef struct edge_t{
}edge_t;
edge_t m[M][M];
void dijkstra(int v0,int d[M],int p[M]){
int i,j,k,v=0,min,min_p;
for(;v<n;v++){
}
for(i=1;i<n;i++){
min_p=min=FINITY;
v=k;
min=d[k];
min_p=p[k];
}
}
if(minFINITY)return;
for(k=0;k<n;k++){
if(!fin[k]&&m[v][k].d!=FINITY){
if(min+m[v][k].d<d[k]){
d[k]=min+m[v][k].d;
p[k]=min_p+m[v][k].p;
}
else if(min+m[v][k].dd[k]&&p[k]>min_p+m[v][k].p){
p[k]=min_p+m[v][k].p;
}
}
}
}
}
int main(){
int i,j,t;
int dis[M],pp[M];
int x,a,b,d,p,max[M],Min;
while(scanf("%d%d",&n,&x)&&(n||x)){
for(i=0;i<n;i++)
for(j=0;j<n;j++)
m[i][j].d=m[i][j].p=FINITY;
for(i=0;i<x;i++){
scanf("%d%d%d%d",&a,&b,&d,&p);
if(m[a-1][b-1].d>d){
m[b-1][a-1].d=m[a-1][b-1].d=d;
m[b-1][a-1].p=m[a-1][b-1].p=p;
}
else if(m[a-1][b-1].d==d&&m[a-1][b-1].p>p){
m[b-1][a-1].p=m[a-1][b-1].p=p;
}
}
scanf("%d%d",&a,&b);
dijkstra(a-1,dis,pp);
printf("%d %dn",dis[b-1],pp[b-1]);
}
}
标签:map,min,int,MAX,d%,3790,ACM,杭电,cost 来源: https://blog.csdn.net/weixin_44204693/article/details/121714516