题意:求各个点到起点1的距离是否<=m,满足的话,就按从小大到的顺序输出结果。
分析:dijkstra。求起点到各个点的最短距离和m判断一下就行了。坑爹的是我wa了13次。就是在一个下标出问题的,白白费了4个小时。不认真。
就是下面的sum.原因自己分析。嗨,死在这里了。我把i放在coutnts[]里面了,原因还要说吗????
View Code
// I'm lanjiangzhou //C #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <math.h> #include <time.h> //C++ #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <cctype> #include <stack> #include <string> #include <list> #include <queue> #include <map> #include <vector> #include <deque> #include <set> using namespace std; //*************************OUTPUT************************* #ifdef WIN32 #define INT64 "%I64d" #define UINT64 "%I64u" #else #define INT64 "%lld" #define UINT64 "%llu" #endif //**************************CONSTANT*********************** #define INF 0x3f3f3f3f #define eps 1e-8 #define PI acos(-1.) #define PI2 asin (1.); typedef long long LL; //typedef __int64 LL; //codeforces typedef unsigned int ui; typedef unsigned long long ui64; #define MP make_pair typedef vector<int> VI; typedef pair<int, int> PII; #define pb push_back #define mp make_pair //***************************SENTENCE************************ #define CL(a,b) memset (a, b, sizeof (a)) #define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b)) #define sqr3(a,b,c) sqrt((double)(a)*(a) + (double)(b)*(b) + (double)(c)*(c)) //****************************FUNCTION************************ template <typename T> double DIS(T va, T vb) { return sqr(va.x - vb.x, va.y - vb.y); } template <class T> inline T INTEGER_LEN(T v) { int len = 1; while (v /= 10) ++len; return len; } template <typename T> inline T square(T va, T vb) { return va * va + vb * vb; } // aply for the memory of the stack //#pragma comment (linker, "/STACK:1024000000,1024000000") //end #define maxn 10010 int f,p,m,c; int edges[maxn][maxn]; int dis[maxn]; int path[maxn]; int counts[maxn]; int sum; int S[maxn]; int pos[maxn]; void dijkstra(int v0){ for(int i=0;i<f;i++){ dis[i]=edges[v0][i]; S[i]=0; // if(i!=v0&&dis[i]<INF) path[i]=v0; //else path[i]=-1; } S[v0]=1; dis[v0]=0; for(int i=0;i<f-1;i++){ int min=INF,u=v0; for(int j=0;j<f;j++){ if(!S[j]&&dis[j]<min){ u=j; min=dis[j]; } } S[u]=1; for(int k=0;k<f;k++){ if(!S[k]&&edges[u][k]<INF&&dis[u]+edges[u][k]<dis[k]){ dis[k]=dis[u]+edges[u][k]; // path[k]=u; } } } } int main(){ scanf("%d%d%d%d",&f,&p,&c,&m); for(int i=0;i<f;i++){ for(int j=0;j<f;j++){ edges[i][j]=INF; } edges[i][i]=0; counts[i]=0; } int u,v,w; sum=0; for(int i=0;i<p;i++){ scanf("%d%d%d",&u,&v,&w); u--; v--; if(edges[u][v]>w) { edges[u][v]=w; edges[v][u]=w; } } dijkstra(0); int x; for(int i=0;i<c;i++){ scanf("%d",&x); x--; if(dis[x]<=m){ //sum++; counts[sum++]=i+1; } } printf("%d\n",sum); for(int i=0;i<sum;i++){ printf("%d\n",counts[i]); } return 0; }
还可以用spaf做,时间16s比上面的dijkstra32s快点。
View Code
// I'm lanjiangzhou //C #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <math.h> #include <time.h> //C++ #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <cctype> #include <stack> #include <string> #include <list> #include <queue> #include <map> #include <vector> #include <deque> #include <set> using namespace std; //*************************OUTPUT************************* #ifdef WIN32 #define INT64 "%I64d" #define UINT64 "%I64u" #else #define INT64 "%lld" #define UINT64 "%llu" #endif //**************************CONSTANT*********************** #define INF 0x3f3f3f3f #define eps 1e-8 #define PI acos(-1.) #define PI2 asin (1.); typedef long long LL; //typedef __int64 LL; //codeforces typedef unsigned int ui; typedef unsigned long long ui64; #define MP make_pair typedef vector<int> VI; typedef pair<int, int> PII; #define pb push_back #define mp make_pair //***************************SENTENCE************************ #define CL(a,b) memset (a, b, sizeof (a)) #define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b)) #define sqr3(a,b,c) sqrt((double)(a)*(a) + (double)(b)*(b) + (double)(c)*(c)) //****************************FUNCTION************************ template <typename T> double DIS(T va, T vb) { return sqr(va.x - vb.x, va.y - vb.y); } template <class T> inline T INTEGER_LEN(T v) { int len = 1; while (v /= 10) ++len; return len; } template <typename T> inline T square(T va, T vb) { return va * va + vb * vb; } // aply for the memory of the stack //#pragma comment (linker, "/STACK:1024000000,1024000000") //end const int maxn = 10000+10; int f,p,c,m;//f农场数,p:边数,c:牛数,m:第m秒(距离) int tot; struct node{ int to; int next; int weight; }; node edges[maxn]; int head[maxn]; int vis[maxn]; int src;//起点 int dis[maxn]; int can[maxn]; int sum; int loc[maxn]; void add(int a,int b,int cost){ edges[tot].to=b; edges[tot].weight=cost; edges[tot].next=head[a]; head[a]=tot++; } void spfa(){ //初始化 for(int i=1;i<=f;i++){ dis[i]=INF; vis[i]=0; } dis[src]=0; vis[src]=1; queue<int> Q; Q.push(src); while(!Q.empty()){ int u=Q.front(); Q.pop(); int v; vis[u]=0; for(int i=head[u];i!=-1;i=edges[i].next){ v=edges[i].to; if(dis[v]>dis[u]+edges[i].weight){ dis[v]=dis[u]+edges[i].weight; if(!vis[v]){ Q.push(v); vis[v]=1; } } } } } int main(){ while(scanf("%d%d%d%d",&f,&p,&c,&m)!=EOF){ int a,b,cost; tot=0; // for(int i=1;i<=f;i++){ // edges[i].weight=INF; // } memset(loc,0,sizeof(loc)); for(int i=1;i<=f;i++){ head[i]=-1; } for(int i=1;i<=p;i++){ scanf("%d%d%d",&a,&b,&cost); add(a,b,cost); add(b,a,cost); } src=1; spfa(); sum=1; for(int i=1;i<=c;i++){ scanf("%d",&loc[i]); if(dis[loc[i]]<=m){ can[sum]=i; sum++; } } sum--; printf("%d\n",sum); for(int i=1;i<=sum;i++){ printf("%d\n",can[i]); } } return 0; }