您的位置:首页 > 其它

1111. Online Map (30)

2016-04-07 20:51 489 查看
题目链接

#include <iostream>
#include<stdio.h>
#include<limits.h>
#include<vector>

#define MAX 501
using namespace std;
typedef struct Map
{
int one_way;
int length;
int time;
}MGraph;
MGraph graph[MAX][MAX];
int curTime=0,curLength=0;
int minTime=INT_MAX,minLength=INT_MAX,minLT=INT_MAX,minTL=INT_MAX;
bool isReached[MAX];
vector<int> minLRoad,minTRoad,curRoad;
///只有一条合格的路
void DFS(int cur,int end)
{
if(curLength>minLength&&curTime>minTime)
return;
if(cur==end)
{
bool lengthBool=false,timeBool=false;

if(curLength<minLength)
{

lengthBool=true;
}
else if(curLength==minLength)
{
if(curTime<minLT)
lengthBool=true;

}
if(lengthBool)
{
minLength=curLength;
minLRoad=curRoad;
minLT=curTime;
}

if(curTime<minTime)
{
timeBool=true;
}
else if(curTime==minTime)
{
if(curLength<minTL)
timeBool=true;
}
if(timeBool)
{
minTime=curTime;
minTRoad=curRoad;
minTL=curLength;
}
return;
}
for(int i=0;i<MAX;i++)
{
if(!isReached[i]&&(graph[cur][i]).time>0)///v1 to v2
{
isReached[i]=true;
curLength+=graph[cur][i].length;
curTime+=graph[cur][i].time;
curRoad.push_back(i);
DFS(i,end);
curRoad.pop_back();
curLength-=graph[cur][i].length;
curTime-=graph[cur][i].time;
isReached[i]=false;
}
}
}
int main()
{
int N,M;

scanf("%d%d",&N,&M);
while(M--)
{
int v1,v2,one_way,length,time;
scanf("%d%d%d%d%d",&v1,&v2,&one_way,&length,&time);

graph[v1][v2].length=length;
graph[v1][v2].time=time;
if(one_way==0)
{
graph[v2][v1].length=length;
graph[v2][v1].time=time;
}

}
int start,end;
scanf("%d%d",&start,&end);
DFS(start,end);
if(minTRoad!=minLRoad)
{

cout<<"Distance = "<<minLength<<": "<<start;

for(int i=0;i<minLRoad.size();i++)
{
cout<<" -> "<<minLRoad[i];
}

cout<<endl;
cout<<"Time = "<<minTime<<": "<<start;
for(int i=0;i<minTRoad.size();i++)
{
cout<<" -> "<<minTRoad[i];
}
}
else {
cout<<"Distance = "<<minLength<<"; ";
cout<<"Time = "<<minTime<<": "<<start;
for(int i=0;i<minTRoad.size();i++)
{
cout<<" -> "<<minTRoad[i];
}
}

return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: