标签:step scan set pre direct ide 路径 dem get
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains four space-separated entities, F1,
F2, L, and D that describe a road. F1 and F2 are numbers of
two farms connected by a road, L is its length, and D is a
character that is either ‘N‘, ‘E‘, ‘S‘, or ‘W‘ giving the
direction of the road from F1 to F2.
Output
Sample Input
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S
Sample Output
52
题目大意:从点A到点B的距离是C,有N个点,M条线,问两点之间最远的距离;
思路:
首先按要存图,一开开始是用的数组,一直RE后来才发现数的范围是1E5,二维数组直接就蹦了,所以要用Vector存图,输入的时候也要用c语言的常规输入输出,不然会TLE
#include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; int n,m;//m个农场,6条路径 struct stu{ int a,b;//保存点2和点1 到点2点距离 }e; vector<stu >arr[100100]; int mark[100010];//标记数组 int ans=0; int xx; void dfs(int x,int step){ if(step>ans){ ans=step; xx=x; } for(int i=0;i<arr[x].size();i++){//arr[x]中的点肯定是与x相连接的点 if(mark[arr[x][i].a]==0){ mark[arr[x][i].a]=1; dfs(arr[x][i].a,step+arr[x][i].b); mark[arr[x][i].a]=0; } } } int main() { scanf("%d%d",&n,&m); int x,y,z; char s; memset(arr,0,sizeof(arr)); for(int i=0;i<m;i++){ // scanf("%d%d%d %c\n",&x,&y,&z); // cin>>x>>y>>z>>s; scanf("%d %d %d",&x,&y,&z); getchar(), getchar(); // getchar(); // getchar();getchar(); arr[x].push_back({y,z}); arr[y].push_back({x,z}); // arr[x][y]=z; // arr[y][x]=z; } ans=0; memset(mark,0,sizeof(mark)); mark[1]=1; dfs(1,0); memset(mark,0,sizeof(mark)); mark[xx]=1; dfs(xx,0);//两轮dfs直接输出 cout<<ans<<endl; return 0; }
标签:step scan set pre direct ide 路径 dem get
原文地址:https://www.cnblogs.com/Accepting/p/11243635.html