1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=10005; int n,m,k,dist[maxn][2],inque[maxn],ans=0x7fffffff/2; string Name; struct Edge { int from,to,dist,bj; } edge[10*maxn]; vector<Edge>edges[maxn]; void AddEdge(const Edge& e) { edges[e.from].push_back(e); } void Spfa(int s) { memset(dist,0x3f,sizeof(dist)); dist[s][1]=dist[s][0]=0; inque[s]=1; queue<int>Q; Q.push(s); while(!Q.empty()) { int Now=Q.front(); Q.pop(); inque[Now]=0; for(Edge& e:edges[Now]) { int Next=e.to; if(e.bj!=2) { if(dist[Next][0]>dist[Now][1]+e.dist) { dist[Next][0]=dist[Now][1]+e.dist; if(!inque[Next]) { inque[Next]=1; Q.push(Next); } } } if(e.bj!=1) { if(dist[Next][1]>dist[Now][0]+e.dist) { dist[Next][1]=dist[Now][0]+e.dist; if(!inque[Next]) { inque[Next]=1; Q.push(Next); } } } } } } int main() { n=Get_Int(); m=Get_Int(); for(int i=1; i<=m; i++) { int x=Get_Int(),y=Get_Int(),v=Get_Int(); edge[i]=(Edge) {y,x,v}; } int tmp=Get_Int(); for(int i=1; i<=tmp; i++)edge[Get_Int()].bj++; tmp=Get_Int(); for(int i=1; i<=tmp; i++)edge[Get_Int()].bj+=2; for(int i=1; i<=m; i++)AddEdge(edge[i]); Spfa(n); k=Get_Int(); for(int i=1; i<=k; i++) { int x=Get_Int(); char s[25]; scanf("%s",s); string name=s; if(ans>dist[x][1]||(ans==dist[x][1]&&name<Name)) { ans=dist[x][1]; Name=name; } } cout<<Name<<endl<<ans<<endl; return 0; }
|